Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue selecting multiple columns from related table in v10 #1737

Closed
wavded opened this issue Sep 21, 2020 · 4 comments
Closed

Issue selecting multiple columns from related table in v10 #1737

wavded opened this issue Sep 21, 2020 · 4 comments

Comments

@wavded
Copy link

wavded commented Sep 21, 2020

I'm having an issue upgrading from v9 to v10. Before, I could select specific columns from the related table (this is a many2many) like so:

db.Model(record).
  Column("record.*"). // Select all columns from main table.
  Column("Related.id", "Related.name"). // Select specific columns from related table.
  WherePK().
  Select()

From my reading it seems like the replacement for this should be:

db.Model(record).
  Relation("Related", func(q *orm.Query) (*orm.Query, error) {
    return q.Column("id", "name"), nil
  }).
  WherePK().
  Select()

However this gives me warning such as "ERROR #42702 column reference "id" is ambiguous". What is the recommended way to select specific columns from a related table? Or is there a bug somewhere?

@wavded
Copy link
Author

wavded commented Sep 21, 2020

Note this is may be related to #1562

@wavded
Copy link
Author

wavded commented Sep 21, 2020

I was able to get the same or very similar behavior as v9 by doing the following. It produce a single query:

db.Model(record).
  Relation("Related.id").
  Relation("Related.name").
  WherePK().
  Select()

Given Relation doesn't take a spread of columns like Column did, I had to call Relation for every field.

@vmihailenco
Copy link
Member

vmihailenco commented Sep 22, 2020

I would try:

    return q.Column("Related.id", "Related.name"), nil

or

    return q.Column("your_alias.id", "your_alias.name"), nil

Closing since there is no reproducer and guessing is no fun.

@rustjson
Copy link

I would try:

    return q.Column("Related.id", "Related.name"), nil

or

    return q.Column("your_alias.id", "your_alias.name"), nil

Closing since there is no reproducer and guessing is no fun.

This doesn't work 👎 , This is simply ADD two same field in the query, but the query already include all the fileds!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants