Reputation: 206
I have the following models:
type User struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name" orm:"null"`
FullName *string `json:"full_name"`
Posts []*Post `orm:"rel(m2m);rel_through(demo/db/models.UserPosts)" json:"posts"`
}
and
type UserPosts struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
Post *Post `orm:"rel(fk);column(post_id)" json:"post"`
User *User `orm:"rel(fk);column(users_id)" json:"user"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
IsInactive bool `json:"is_inactive"`
}
and
type Post struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
Content *string `json:"content"`
Type *string `json:"type" orm:"null"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
IsInactive bool `json:"is_inactive"`
Users []*User `orm:"reverse(many)" json:"users"`
}
I want to order the distinct users queried via the created_at field of the Post table.
But whenever I run the following query
import "github.com/astaxie/beego/orm"
db := orm.NewOrm()
qs := db.QueryTable(new(models.User))
num, err := qs.Distinct().RelatedSel().All(&users)
The query formed by beego does not contain the field created_at of Post table and hence it gives an error. The query for the same is listed below:
SELECT DISTINCT T0."id", T0."first_name", T0."last_name", T0."full_name"
FROM "users" T0
INNER JOIN "user_posts" T1
ON T1."users_id" = T0."id"
WHERE T1."tenants_id"
IN $1
AND T1."is_inactive" = $2
ORDER BY T1."created_at" DESC ;
I checked through the documentation but wasn't able to find any hint to the solution.
NOTE: The disctinct is required due to other constraints I have in my code which I have not highlighted in the question since that is out of scope
Hoping someone can point me in the right direction to achieve the same.
Upvotes: 1
Views: 664