2

Playing around with GO/Beego Framework and trying to query the database to load some records into a struct.

Bellow is the important code:

type User struct {
    UserId int64 `orm:"pk"`
    FirstName string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    LastName string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    Email string `valid:"Required;MinSize(2);MaxSize(150);Email" required:"true" description:"user email address"`
    Password string `valid:"Required;MaxSize(60)"  required:"true" description:"user plain text password" json:"-"`
    AccessLevel uint64 `json:"-"`
    AuthKey string `json:"-"`
    Status int `json:"-"`
    DateAdded time.Time `orm:"-" json:"-"`
    LastUpdated time.Time `orm:"-" json:"-"`

    // relations
    Profile *UserProfile `orm:"rel(one)" json:"-"` // OneToOne relation
}

type UserProfile struct {
    UserId  int64 `orm:"pk"` // doesn't work without a PK, doesn't make sense, it's a fk...
    Company string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    VatNumber string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    Website string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    Phone string `orm:"null" valid:"MinSize(2);MaxSize(150);Mobile"`
    Address1 string `orm:"null" valid:"MinSize(2);MaxSize(255)"`
    Address2 string `orm:"null" valid:"MinSize(2);MaxSize(255)"`
    City string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    State string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    Zip string `orm:"null" valid:"MinSize(4);MaxSize(15)"`
    Country string `orm:"null" valid:"MinSize(2);MaxSize(150)"`
    ConfirmationKey string `orm:"null" valid:"Length(40)" json:"-"`

    // relations
    User *User `orm:"reverse(one)" json:"-"` // Reverse relationship (optional)
}

func GetAllUsers() []User {
    o := orm.NewOrm()
    var users []User
    sql := `
    SELECT user.*, user_profile.* 
       FROM user
       INNER JOIN user_profile ON user_profile.user_id = user.user_id 
    WHERE 1`
    _, err := o.Raw(sql).QueryRows(&users)
    if err != nil {
        beego.Error(err)
    }
    beego.Info(users)
    return users
}

Now the problem with above is that upon calling GetAllUsers() the embed struct from User, that is Profile, doesn't get populated, so how would i go so that embed structs are also populated?

I have also tried to get the users with:

o.QueryTable("user").Filter("status", STATUS_ACTIVE).RelatedSel("Profile").All(&users)

which produces a sql query like:

SELECT T0.`user_id`, T0.`first_name`, T0.`last_name`, T0.`email`, T0.`password`, T0.`access_level`, T0.`auth_key`, T0.`status`, T0.`profile_id`, T1.`user_id`, T1.`company`, T1.`vat_number`, T1.`website`, T1.`phone`, T1.`address1`, T1.`address2`, T1.`city`, T1.`state`, T1.`zip`, T1.`country`, T1.`confirmation_key` FROM `user` T0 INNER JOIN `user_profile` T1 ON T1.`user_id` = T0.`profile_id` WHERE T0.`status` = ? LIMIT 1000

And i don't know from where it came up with the join on profile_id column since that doesn't even exists and i am not sure how to specify the right column to join, seems it takes it from the structure name. Also, i don't like the fact that there isn't a way to specify what to select.

Any hint is highly appreciated, i am sure it's something simple that i miss.

Thanks.

Twisted1919
  • 2,430
  • 1
  • 19
  • 30
  • Perhaps it was due to cut-n-pasting into SO, but please [`gofmt` all Go code](https://blog.golang.org/go-fmt-your-code). In this case it would have made the type definitions much easier to read. – Dave C Apr 23 '15 at 17:20

1 Answers1

1

https://github.com/astaxie/beego/issues/384 has a relative talk, but it's Chinese.The key is below:

type User struct {
  Id   int
  Name string
}

type Profile struct {
  Id   int
  Age  int
}

var users []*User
var profiles []*Profile
err := o.Raw(`SELECT id, name, p.id, p.age FROM user
    LEFT OUTER JOIN profile AS p ON p.id = profile_id WHERE id = ?`, 1).QueryRows(&users, &profiles)
wuranbo
  • 101
  • 7