0

I'm using Beego ORM for my database accesses. Currently I'm trying to write a query that joins two tables, user and account, and only returns certain columns of the resulting table.

Something like this:

SELECT Email, FirstName, LastName, Organisation
FROM user INNER JOIN account ON user.accountId = account.Id
WHERE Activated = 0

This is what I have so far:

o.QueryTable("user").Filter("Activated", 0).RelatedSel().All(&u, "Email", "FirstName", "LastName")

Now it returns only the columns I specified in the All() function for the user table. However, for the 'account' table it joins on it returns everything. The documentation says this:

qs.RelatedSel("user") // INNER JOIN user, Only query the fields set by expr

If I do this like so:

o.QueryTable("user").Filter("Activated", 0).RelatedSel("organisation").All(&u, "Email", "FirstName", "LastName")

where 'organisation' is a column in the account table, it actually trys to join on organisation which is not a table. How can I get my intended result?

Gilrich
  • 305
  • 3
  • 13
  • I'm still having this problem. As I quoted above the doc says: >Only query the fields set by expr. expr is defined here: https://beego.me/docs/mvc/model/query.md#expr But if I use 'account__organisation' it still queries for the table 'organisation' instead for the field. – Gilrich Jun 26 '17 at 10:16
  • I also tried using raw queries but to no avail. When using raw queries like this: o.Raw("SELECT Email, FirstName, LastName, Organisation FROM user INNER JOIN account ON user.accountId = account.Id WHERE Activated = ?, 0).QueryRows(&u) the account reference is nil. – Gilrich Jun 28 '17 at 16:44

0 Answers0