使用GORM创建带有外键的记录。

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

GORM create record with foreign key

问题

无法创建引用现有行的记录。

我有两个表,其中一个表引用另一个表。表"Category"已经存在。我尝试在"Heritage"表中添加新记录,但gorm尝试在Category中创建新记录。

type Category struct {
    gorm.Model
    Name string `gorm:"unique"`
}

type Site struct {
    gorm.Model
    Category         Category `gorm:"foreignkey:CategoryID"`
    DateInscribed    string
    HttpURL          string
    Latitude         float32
    Location         string
    Longitude        float32
    SecondaryDates   string
    ShortDescription string
    Site             string
}

正如我所说,"Category"表已经存在,并且具有我需要的所有行。我尝试创建Site记录,但出现问题"define a valid foreign key for relations or implement the Valuer/Scanner interface"。

使用以下代码,我尝试创建记录。

site := &models.Site{
    Category:         models.Category{Name: heritage.Category},
    DateInscribed:    heritage.DateInscribed,
    HttpURL:          heritage.HttpURL,
    Latitude:         heritage.Latitude,
    Location:         heritage.Location,
    Longitude:        heritage.Longitude,
    SecondaryDates:   heritage.SecondaryDates,
    ShortDescription: heritage.ShortDescription,
}

db.Create(site)

看起来我尝试在Category表上创建新记录,但只需要将category_id值添加到Site记录中。

英文:

Can't make record that refers to existed row.

I have two tables and one table references on another one. Table "Category" is already existed. I try to add new record on "Heritage" table but gorm try to do new record at Category.

type Category struct {
	gorm.Model
	Name string `gorm:"unique"`
}

type Site struct {
	gorm.Model
	Category         Category `gorm:"foreignkey:CategoryID"`
	DateInscribed    string
	HttpURL          string
	Latitude         float32
	Location         string
	Longitude        float32
	SecondaryDates   string
	ShortDescription string
	Site             string
}

As I say "Category" table is existed with all I needed rows. I try to do Site record but get issue "define a valid foreign key for relations or implement the Valuer/Scanner interface".

With this code I try to make record.

site := &models.Site{
	Category:         models.Category{Name: heritage.Category},
	DateInscribed:    heritage.DateInscribed,
	HttpURL:          heritage.HttpURL,
	Latitude:         heritage.Latitude,
	Location:         heritage.Location,
	Longitude:        heritage.Longitude,
	SecondaryDates:   heritage.SecondaryDates,
	ShortDescription: heritage.ShortDescription,
}

db.Create(site)

Seems I try to make new record on Category table but needed only add category_id value to Site record.

答案1

得分: 1

通常情况下,你不会将Name作为外键存储,而是使用ID值。为此,你需要在Site结构体中添加一个新字段CategoryID,类型为uint

type Site struct {
    gorm.Model
    Category         Category
    CategoryID       uint
    DateInscribed    string
    ...
}

这个字段将自动与Category表关联,所以如果你使用默认名称,就不需要写gorm:"foreignKey:CategoryID"

然后,要创建一个Site对象,你可以这样做:

site := &models.Site{
    CategoryID: id,
    DateInscribed: heritage.DateInscribed,
    ...
}
db.Create(site)

如果你想重用现有的Category表,你需要通过名称查找ID:

var id uint
db.Model(&models.Category{}).Where("name = ?", name).Select("ID").First(&id)

对于我来说,从GORM文档上并不清楚如果你想引用Name字段应该怎么做,也许其他人可以回答。

希望对你有所帮助。

英文:

Normally you don't store the Name as a foreign key, but the ID value.
For that you need to add a new field CategoryID in Site of type uint:

type Site struct {
    gorm.Model
    Category         Category
    CategoryID       uint
    DateInscribed    string
    ...
}

This field will be automatically linked to the Category table, so if you use the default name, you don't need to write gorm:"foreignKey:CategoryID".

Then to create a Site object, you can do it in this way:

site := &models.Site{
	CategoryID: id,
    DateInscribed:    heritage.DateInscribed,
    ...
}
db.Create(site)

If you want to reuse the existing table Category, you will need to find the ID from name:

var id uint
db.Model(&models.Category{}).Where("name = ?", name).Select("ID").First(&id)

For me it is not clear from the GORM documentation what to do if you want to reference the Name field, maybe someone else can answer.

Hope this helps.

huangapple
  • 本文由 发表于 2023年2月14日 13:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75443819.html
匿名

发表评论

匿名网友

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

确定