package models
import "github.com/beego/beego/v2/client/orm"
type Customer struct {
Id int `orm:"pk;auto""`
UserName string
Sex string `orm:"size(5)"`
Mobile string `orm:"size(11)"`
Order []*Order `orm:"reverse(many)"` // one to many
}
type Order struct {
Id int `orm:"pk;auto"`
OrderData string
Customer *Customer `orm:"rel(fk)"` // 外键
Products []*Product `orm:"rel(m2m)"` //many to many
}
type Product struct {
Id int `orm:"pk;auto""`
Name string
Price float64
Order []*Order `orm:"reverse(many)"`
}
type News struct {
Id int `orm:"pk;auto""`
Title string `json:"title"`
Content string `json:"content"`
Type *NewsType `orm:"rel(fk)"` // 外键
}
type NewsType struct {
Id int `orm:"pk;auto""`
Name string
}
func GetNewsWithType() ([]*News, error) {
o := orm.NewOrm()
var news []*News
_, err := o.QueryTable("news").RelatedSel().All(&news)
if err != nil {
return nil, err
}
return news, nil
}
-------------------------------
标签:Beego,string,int,查询,Orm,orm,pk,type,struct From: https://www.cnblogs.com/superzwb/p/18187072