GORM AutoMigrate 有一个和多个:
GORM AutoMigrate Has One & Has Many:
我想创建一个模型 User
和 Social
,其中 User
模型有很多 Socials
。理想情况下, Social
类型也应该有一个关系来简化来自任何一方的查询。这是一个代码示例:
数据库类型为MySQL8.0
type base struct {
ID string `json:"id" gorm:"type:char(36);primaryKey;"`
Created time.Time `json:"created" gorm:"autoCreateTime"`
Updated time.Time `json:"updated" gorm:"autoUpdateTime"`
}
type User struct {
base
Friends []*User `json:"friends" gorm:"many2many:friends"`
Socials []*Social `json:"socials"`
}
type Social struct {
base
Provider string `json:"provider" gorm:"type:varchar(32);index"`
Identifier string `json:"identifier" gorm:"type:varchar(32);index"`
User *User `json:"user" gorm:"foreignKey:ID"`
Token string `json:"token"`
Link string `json:"link" gorm:"type:varchar(128)"`
}
我在使用 db.AutoMigrate(&User{}, &Social{})
时遇到以下错误:
model.Social's field User, need to define a valid foreign key for relations or it need to implement the Valuer/Scanner interface
runtime error: invalid memory address or nil pointer dereference
我试过:
- 将
gorm:"foreignKey:ID"
添加到 User.Socials 标签
- 不使用指针(例如在
User
结构 Socials []Social
中而不是 Socials []*Social
)
但问题依然存在
根据文档 (https://gorm.io/docs/has_many.html#Has-Many),
你需要使用对象,而不是引用
type User struct {
base
Friends []User `json:"friends" gorm:"many2many:friends"`
Socials []Social `json:"socials"`
}
这里没有*
您还可以将 UserID
字段添加到 Social
type Social struct {
base
UserID string
Provider string `json:"provider" gorm:"type:varchar(32);index"`
Identifier string `json:"identifier" gorm:"type:varchar(32);index"`
User *User `json:"user" gorm:"foreignKey:ID"`
Token string `json:"token"`
Link string `json:"link" gorm:"type:varchar(128)"`
}
并添加
type User struct {
base
FriendOf string `gorm:""`
Friends []*User `json:"friends" gorm:"many2many:friends,foreignKey:FriendOf"`
Socials []*Social `json:"socials"`
}
问题在这里:
type base struct {
...
}
type User {
base
...
}
type Social {
base
...
}
因为我认为 base
只是包本地定义,所以我弄乱了大写并有一个私有主键。
@vodolaz095 触及了另一个问题,但 (imo) 没有为任何新 go-gorm
用户充分阐明。
似乎无法使用像 User User
这样的 has one
关系作为像 Socials []Social gorm:"foreignKey:User"
这样的 has many
关系的外键。需要拆分为他的第二个代码块中显示的@vodolaz095
我想创建一个模型 User
和 Social
,其中 User
模型有很多 Socials
。理想情况下, Social
类型也应该有一个关系来简化来自任何一方的查询。这是一个代码示例:
数据库类型为MySQL8.0
type base struct {
ID string `json:"id" gorm:"type:char(36);primaryKey;"`
Created time.Time `json:"created" gorm:"autoCreateTime"`
Updated time.Time `json:"updated" gorm:"autoUpdateTime"`
}
type User struct {
base
Friends []*User `json:"friends" gorm:"many2many:friends"`
Socials []*Social `json:"socials"`
}
type Social struct {
base
Provider string `json:"provider" gorm:"type:varchar(32);index"`
Identifier string `json:"identifier" gorm:"type:varchar(32);index"`
User *User `json:"user" gorm:"foreignKey:ID"`
Token string `json:"token"`
Link string `json:"link" gorm:"type:varchar(128)"`
}
我在使用 db.AutoMigrate(&User{}, &Social{})
时遇到以下错误:
model.Social's field User, need to define a valid foreign key for relations or it need to implement the Valuer/Scanner interface
runtime error: invalid memory address or nil pointer dereference
我试过:
- 将
gorm:"foreignKey:ID"
添加到 User.Socials 标签 - 不使用指针(例如在
User
结构Socials []Social
中而不是Socials []*Social
)
但问题依然存在
根据文档 (https://gorm.io/docs/has_many.html#Has-Many), 你需要使用对象,而不是引用
type User struct {
base
Friends []User `json:"friends" gorm:"many2many:friends"`
Socials []Social `json:"socials"`
}
这里没有*
您还可以将 UserID
字段添加到 Social
type Social struct {
base
UserID string
Provider string `json:"provider" gorm:"type:varchar(32);index"`
Identifier string `json:"identifier" gorm:"type:varchar(32);index"`
User *User `json:"user" gorm:"foreignKey:ID"`
Token string `json:"token"`
Link string `json:"link" gorm:"type:varchar(128)"`
}
并添加
type User struct {
base
FriendOf string `gorm:""`
Friends []*User `json:"friends" gorm:"many2many:friends,foreignKey:FriendOf"`
Socials []*Social `json:"socials"`
}
问题在这里:
type base struct {
...
}
type User {
base
...
}
type Social {
base
...
}
因为我认为 base
只是包本地定义,所以我弄乱了大写并有一个私有主键。
@vodolaz095 触及了另一个问题,但 (imo) 没有为任何新 go-gorm
用户充分阐明。
似乎无法使用像 User User
这样的 has one
关系作为像 Socials []Social gorm:"foreignKey:User"
这样的 has many
关系的外键。需要拆分为他的第二个代码块中显示的@vodolaz095