I have an interface type named Invoice
:
type Invoice interface {
GetTransactions() ([]Transaction, error)
}
type DebitInvoice struct {
gorm.Model
ClientID uint `json:"client_id"`
Client Store `json:"client"`
Debt uint `json:"debt"` //REMAINING PAYMENT
}
func (d DebitInvoice) GetTransactions() ([]Transaction, error) {
// process
return transactions, nil
}
type CreditInvoice struct { //PEMBELIAN
gorm.Model
InvoiceCreditID string `json:"invoice_credit_id"`
StoreID uint `json:"store_id"`
Store Store `json:"store"`
Debt uint `json:"debt"` //REMAINING PAYMENT
}
func (c CreditInvoice) GetTransactions() ([]Transaction, error) {
// process
return transactions, nil
}
as you can see, the DebitInvoice
and CreditInvoice
are Invoice
because it implements GetTransactions()([]Transactions, error)
.
and I have this kind of model (in struct) where we are using Invoice
interface as one of the field
type Transaction struct {
gorm.Model
InventoryID uint `json:"inventory_id"`
Inventory Inventory `json:"inventory"`
InvoiceID uint `json:"invoice_id"`
InvoiceType string `json:"invoice_type"` //Credit/Debit (Out/In)
Invoice Invoice `json:"invoice"`
TotalPrice uint `json:"total_price"`
Discount uint `json:"discount"`
}
But when I migrate this with GORM AutoMigrate. It doesn't work, it shows unsupported data type: program_akuntansi/models.Invoice
and the transactions
table is not created
My expectations with Transaction
model is :
Invoice
field will create a new record to their respective tableInvoiceType
field is the name of table that invoice createdInvoiceID
field is the ID of the record inInvoiceType
table
but none of it doesn't work
So back to my question, how does the Interface Type work with GORM and how do I fix my code?