Implementing a relationship with an array of values in Gorm

huangapple go评论86阅读模式
英文:

Implementing a relationship with an array of values in Gorm

问题

我正在尝试使用Go和Gorm实现一个发票应用程序的模型。我已经定义了Invoice结构体,并希望从一个单独的结构体中包含发票行项目。

这是我的Invoice结构体:

type Invoice struct {
    Base
    CompanyID  string `gorm:"not null"`
    Company    Company
    InvoiceNo  string     `gorm:"not null"`
    Currency   string     `gorm:"not null;default:'GBP'"`
    Total      float64    `gorm:"not null"`
    Terms      int        `gorm:"not null;default:30"`
    IssuedDate time.Time  `gorm:"not null"`
    DueDate    time.Time  `gorm:"not null"`
    Paid       bool       `gorm:"not null"`
    LineItems  []LineItem `gorm:"foreignKey:InvoiceID"`
    Void       bool       `gorm:"default:false"`
}

这是我的LineItem结构体:

type LineItem struct {
    Base
    Service     string `gorm:"not null"`
    Description string `gorm:"not null;"`
    Amount      float64
    Count       int
    UnitBase    string  `gorm:"not null;default:'Days'"` // days or hours
    Total       float64 `gorm:"not null"`
}

当我尝试构建应用程序时,我收到以下错误:

... got error invalid field found for struct github.com/repo/API/database/models.Invoice's field LineItems: define a valid foreign key for relations or implement the Valuer/Scanner interface

我的想法是,我可以定义一个有固定费率和描述可供选择的有限行项目集,以限制重复。

我不确定我是否以正确的方式进行操作。所以我的问题是,是否可以以这种方式在关系模型中包含一个项目数组?

英文:

I'm trying to implement a model for an invoicing application using Go and Gorm. I have defined my Invoice struct and want to include the invoice line items from a separate struct.

type Invoice struct {
	Base
	CompanyID  string `gorm:"not null"`
	Company    Company
	InvoiceNo  string     `gorm:"not null"`
	Currency   string     `gorm:"not null;default:'GBP'"`
	Total      float64    `gorm:"not null"`
	Terms      int        `gorm:"not null;default:30"`
	IssuedDate time.Time  `gorm:"not null"`
	DueDate    time.Time  `gorm:"not null"`
	Paid       bool       `gorm:"not null"`
	LineItems  []LineItem `gorm:"foreignKey:InvoiceID"`
	Void       bool       `gorm:"default:false"`
}

This is my LineItem struct.

type LineItem struct {
	Base
	Service     string `gorm:"not null"`
	Description string `gorm:"not null;"`
	Amount      float64
	Count       int
	UnitBase    string  `gorm:"not null;default:'Days'"` // days or hours
	Total       float64 `gorm:"not null"`
}

When I try to build the application I get the below error.

... got error invalid field found for struct github.com/repo/API/database/models.Invoice's field LineItems: define a valid foreign key for relations or implement the Valuer/Scanner interface

The idea is that I can define a limited set up line items which can be chosen from with fixed rates and descriptions to limit repetition.

I'm unsure if I'm going about this the right/correct way.

So my question is, is it possible to include an array of items from a relational model in this way?

答案1

得分: 2

根据你的列名,我可以想到三种方法来实现这个:

1)使用默认语法(没有gorm:foreignKey

type Invoice struct {
    ID         // 这是主键,也可能在你的基本模型中
    Lineitems []LineItem
    ..其他字段
}

type LineItem struct {
    ID          // LineItem的主键
    InvoiceID   // 这将自动成为你的外键
    ..其他字段
}

2)指定自定义外键(假设第二个结构体有一个不同的列名,你想要它作为外键)

type Invoice struct {
    ID         // 这是主键,也可能在你的基本模型中
    Lineitems []LineItem `gorm:"foreignKey:ParentID"`
    ..其他字段
}

type LineItem struct {
    ID          // LineItem的主键
    ParentID    // 这是自定义列,引用了Invoice.ID
    ..其他字段
}

3)或者两个结构体有不同的列名

type Invoice struct {
    ID         // 这是主键,也可能在你的基本模型中
    InvoiceNum 
    Lineitems []LineItem `gorm:"foreignKey:ParentNum;references:InvoiceNum"`
    ..其他字段
}

type LineItem struct {
    ID          // LineItem的主键
    ParentNum   // 现在它引用了Invoice.InvoiceNum
    ..其他字段
}
英文:

Depending on your column names, there are three ways I can think of how you can achieve this:

  1. Use default syntax (No gorm:foreignKey)

     type Invoice struct {
         ID     //this is the primary key, may also be in your base model
         Lineitems []LineItem
         ..other fields
     }
    
     type LineItem struct {
         ID          //primary key of LineItem
         InvoiceID   //automatically this will be your foreign key
         ..other fields
     }
    
  2. Specifying custom foreign key (lets say second struct has a different column name which you want as the foreignKey)

     type Invoice struct {
         ID     //this is the primary key, may also be in your base model
         Lineitems []LineItem `gorm:"foreignKey:ParentID"`
         ..other fields
     }
    
     type LineItem struct {
         ID          //primary key of LineItem
         ParentID   //this is the custom column referencing Invoice.ID
         ..other fields
     }
    
  3. Or both structs have different column names

     type Invoice struct {
         ID     //this is the primary key, may also be in your base model
         InvoiceNum 
         Lineitems []LineItem `gorm:"foreignKey:ParentNum;references:InvoiceNum"`
         ..other fields
     }
    
     type LineItem struct {
         ID          //primary key of LineItem
         ParentNum   //this is now referencing Invoice.InvoiceNum
         ..other fields
     }
    

huangapple
  • 本文由 发表于 2023年2月21日 02:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75512970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定