【go新手】beego关联关系的一个坑

先建Users

type Users struct {
    Id          int64           `orm:"column(id);auto;unique" json:"id"`
    Name        string          `orm:"size(128)"`
    Email       string          `orm:"size(128)"`
    Password    string          `orm:"size(128)"`
    Article     []*Articles     `orm:"reverse(many)"`//一对多
}

然后建立Posts

type Articles struct {
    Id           int64     `orm:"column(id);auto;unique" json:"id"`
    Title        string    `orm:"column(title);size(255)" json:"title"`
    Content      string    `orm:"column(content);type(longtext);null" json:"content"`
    BodyOriginal string    `orm:"column(body_original);type(longtext);null" json:"body_original"`
    UserId       int64     `orm:"column(user_id);null;default(0)" json:"user_id"`
    Password     int64     `orm:"column(password);null" json:"password"`
    Note         string    `orm:"column(note);null" json:"note"`
    ReadStatus   int64     `orm:"column(read_status);default(1)" json:"read_status"`
    Top          bool      `orm:"column(top);null;default(false)" json:"top"`
    Abstract     string    `orm:"column(abstract);size(500);null" json:"abstract"`
    ViewNum      int64     `orm:"column(view_num);default(0);null" json:"view_num"`
    CreatedAt    time.Time `orm:"column(created_at);default('0000-00-00 00:00:00');null;auto_now_add;type(datetime)" json:"created_at"`
    UpdatedAt    time.Time `orm:"column(updated_at);default('0000-00-00 00:00:00');null;auto_now;type(datetime)" json:"updated_at"`
    User         *Users    `orm:"rel(fk)" json:"user"`
}

因为是数据迁移,这是建好model

关联查询


    o := orm.NewOrm()
    user := &Users{}
    o.QueryTable("y_users").Filter("Id",1).RelatedSel().One(user)

    fmt.Println(user.Article)

但是 会查不出来!

Handler crashed with error wrong field/column name UserId

说找不到UserId

最后发现,beego的orm 会自动建 关系,所以上面的结构体里的

//UserId int64 orm:"column(user_id);null;default(0)" json:"user_id"

就不能要!否则拿不到数据的!

坑!

我还是手写吧,不用关联关系了! 有毒....


欢迎转载,但请附上原文地址哦,尊重原创,谢谢大家 本文地址: http://www.iphpt.com/detail/111/

当你能力不能满足你的野心的时候,你就该沉下心来学习