type Author struct {
AID int `gorm:"primary_key;AUTO_INCREMENT"`
Name string
Age string
Sex string
//关联关系
Article []Article `gorm:"ForeignKey:Auid;AssociationForeignKey:AID"`
}
type Article struct {
ArId int `gorm:"primary_key;AUTO_INCREMENT"`
Title string
Content string
Desc string
//设置外键
AuId int
}
创建
author := Author{
Name: "张三",
Age: 30,
Sex: "男",
Article: []Article{
{
Title: "HTML入门",
Content: "",
Desc: "",
},
{
Title: "HTML入门",
Content: "",
Desc: "",
},
},
}
db.Create(&author)
查询一
//Association方式查询,因为关联关系在Author