本教材由知了传课辛苦制作而成,仅供学习使用,请勿用于商业用途!如进行转载请务必注明出处!谢谢!

模型定义三

一、一对一

1.属于

// UserProfile属于User,外键是在UserProfile模型,外键字段为:UId type User struct { Id int Name string Age int Addr string } type UserProfile struct { Id int Pic string CPic string Phone string User User `gorm:"ForeignKey:UId;AssociationForeignKey:Id"` // 关联关系 //UserID int // 默认关联字段为Id UId int // uid } 注: // 外键默认使用UserID,如果不指定外键,则使用默认的外键字段, // 默认关联ID,通过AssociationForeignKey指定关联字段

2.包含

// UserProfile 包含一个 User, 外键在User模型,外键字段为:PId type User struct { Id int Name string Age int Addr string PId int } type UserProfile struct { Id int Pic string CPic string Phone string User User `gorm:"ForeignKey:PId;AssociationForeignKey:Id"` // 关联关系 }

属于:关系和外键在同一方,有关系的那一方属于另外一个模型

包含:关系和外键不在同一方,有关系的那一方包含另外一个有外键的模型

二、一对多

type User2 struct { Id int Name string Age int Addr string Articles []Article `gorm:"ForeignKey:UId;AssociationForeignKey:Id"` } type Article struct { Id int Title string Content string Desc string // 外键 UId int }

三、多对多

type Article2 struct { AId int `gorm:"primary_key:true"` Title string Content string Desc string Tags []Tag `gorm:"many2many:Article2s2Tags"` // ;ForeignKey:AId;AssociationForeignKey:TId } type Tag struct { TId int `gorm:"primary_key:true"` Name string Desc string }

创建/更新时不会保存关联:`gorm:“save_associations:false”`

1439人已阅读,今天你学习了吗?

添加新回复
目录