外键在一对多关联中未创建

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

Foreign key not created with one to many association

问题

我有一个Task类型,其中包含一个Runner类型的对象列表。我正在尝试使用golang gorm将其映射到数据库,但它没有外键,而且在迁移过程中出现了“无效的关联”错误。

我的Task结构体:

type Task struct {
    gorm.Model
    Name        string `gorm:"not null;unique_index"`
    Description string
    Runners     []Runner
}

我的Runner结构体:

type Runner struct {
    gorm.Model
    Name        string `gorm:"not null;unique"`
    Description string
}

我的迁移代码:

func migrateSchema() (err error) {
    db, err := context.DBProvider()

    if err != nil {
        return
    }

    db.Model(&Task{}).Related(&Runner{})
    db.AutoMigrate(&Task{})
    db.AutoMigrate(&Runner{})

    return
}

db.AutoMigrate(&Task{})这一行,我在控制台上收到了“无效的关联”消息,并且当我检查数据库时,没有创建外键,也没有在runners表上创建引用字段。

我做错了什么?

英文:

I have a Task type that has a list of Runner type objects in it. I am trying to map it to database using golang gorm but it doesn't have foreign key and i am getting invalid association during migration

My Task struct:

type Task struct {
	gorm.Model
	Name        string `gorm:"not null;unique_index"`
	Description string
	Runners     []Runner
}

My Runner struct:

type Runner struct {
	gorm.Model
	Name       	string `gorm:"not null;unique"`
	Description string
}

My migration code:

func migrateSchema () (err error) {
	db, err := context.DBProvider()

	if err != nil {
		return
	}

	db.Model(&Task{}).Related(&Runner{})
	db.AutoMigrate(&Task{})
	db.AutoMigrate(&Runner{})

	return
}

On db.AutoMigrate(&Task{}) I get invalid association message in console and when I check the database there is no foreign key created or no reference field created on runners table

What am I doing wrong?

答案1

得分: 0

我遇到了类似的问题,花了很长时间才解决。我认为 GORM 的文档肯定可以更好一些。以下是来自 GORM 网站 的相关代码片段:

// User has many emails, UserID is the foreign key
type User struct {
    gorm.Model
    Emails   []Email
}

type Email struct {
    gorm.Model
    Email   string
    UserID  uint
}

db.Model(&user).Related(&emails)
//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key

你的代码为什么不起作用:

  1. 首先,你需要在 Runner 结构体中添加一个 TaskID 字段。
  2. db.Model(&Task{}).Related(&Runner{}) 并不是你想象中的那样工作。如果你看一下 GORM 的代码片段,SELECT 注释有点解释了(虽然不是很清楚)。示例假设 &user 已经被填充,并且具有 ID 为 111,然后它获取与 &userUserID 匹配的 emails,并将它们存储在 &emails 中。
英文:

I had a similar issue, and it took me forever to figure it out. I believe the GORM documentation could definitely be better. Here's the relevant code snippet from the GORM site:

//User has many emails, UserID is the foreign key
type User struct {
    gorm.Model
    Emails   []Email
}

type Email struct {
    gorm.Model
    Email   string
    UserID  uint
}

db.Model(&user).Related(&emails)
//// SELECT * FROM emails WHERE user_id = 111; // 111 is user's primary key

Why your code isn't working:

  1. First you need to add a TaskID field to your Runner struct.
  2. db.Model(&Task{}).Related(&Runner{}) doesn't do what you think it does. If you look at the code snippet from GORM, the SELECT comment kind of explains it (not very well though). The example is assuming that the &user is already populated and has an ID of 111, then it fetches the emails storing them in &emails that match the UserID of &user.

huangapple
  • 本文由 发表于 2017年9月10日 09:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/46136500.html
匿名

发表评论

匿名网友

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

确定