gorm关联时,报错:import cycle not allowed
直接上代码:
用户表结构:
package Users
type Users struct {
Id uint `gorm:"column:id;type:int(11);NOT NULL" json:"id"`
Phone string `gorm:"column:phone;type:char(11);comment:手机号;NOT NULL" json:"phone"`
Nickname string `gorm:"column:nickname;type:varchar(20);comment:昵称;NOT NULL" json:"nickname"`
UserHobby []UsersHobby.UsersHobby `json:"user_hobby"` // 关联用户爱好
}
用户爱好表:
package UsersHobby
type UsersHobby struct {
Id uint `gorm:"column:id;type:int(11);NOT NULL" json:"id"`
UserId uint `gorm:"column:user_id;type:int(10) unsigned;default:0;comment:用户ID;NOT NULL" json:"user_id"`
Hobby string `gorm:"column:hobby;type:varchar(20);comment:爱好内容;NOT NULL" json:"hobby"`
Users Users.Users `json:"users"` // 关联用户表
}
这个时候就会报错:
imports xxx/models/UsersHobby: import cycle not allowed
思想深受PHP影响,不知道怎么封装model和关联层了,感谢指导!
把两个模型放在一个包里