type User struct { Id int orm:pk,auto//默认 Name string orm:size(100) Order []*User_order orm:ref(fk)//外键 } type User_order struct { Id int Ordinfo string orm:size(100)
type User struct {
Id int orm:pk,auto//默认
Name string orm:size(100)
Order []*User_order orm:ref(fk)//外键
}
type User_order struct {
Id int
Ordinfo string orm:size(100)
User *User orm:reverse(many)//反向数据库
}
func Init() {
orm.RegisterDataBase(“default”,”mysql”,”root:itroot@tcp(127.0.0.1:3306)/mytest?charset=utf-8”,30)
orm.RegisterModel(new(User),new(User_order))
//第一个参数是 是否重新创建表
orm.RunSyncdb(“”,true,true)
}
func queryOder() {
var orders []User_order
o := orm.NewOrm()
qs := o.QueryTable(“User_order”)
_,err:=qs.Filter(“user__id”, 1).All(&orders)//两个下划线
if err!=nil {
beego.Error(“query erro”)
}
for _,order:=range orders {
beego.Info(“query order sucess”,order)
}
}