question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

column reference "name" is ambiguous

See original GitHub issue

Hi,

I’m running into an issue with a combination of query and where clauses on a model. I’m running on PostgreSQL database.

The following code is dynamically built depending on the API request:

Brand.query((qb) => {
          qb.innerJoin('partners as p', function() {
            this.on('brands.partner_id', '=', 'p.id')
          });

          qb.where('p.name', 'ILIKE', '%partnername%');
        })
        .where('name','=','brandname')
        .fetch();

results in:

error: select "brands".* from "brands" inner join "partners" as "p" on "brands"."partner_id" = "p"."id" where "p"."name" ILIKE $1 and "name" ILIKE $2 order by "brands"."id" ASC limit $3 - column reference "name" is ambiguous

Again, the where clause is built dynamically from the API request. Is there any way to either prefix the where property with the tablename or to add an alias for the Brand model’s properties?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vellotiscommented, Sep 11, 2016

I have been using plugin that creates a static method field.

module.exports = function (bookshelf, options) {
  bookshelf.Model = bookshelf.Model.extend({}, {
    field: function (fieldName) {
      return this.prototype.tableName + '.' + fieldName;
    }
  });
};

Then it can be used like Model.field('someFieldName'). Eg.

Author.forge().books().query(function (qb) {
  qb.where(Book.field('id'), 2); // WHERE books.id = 2
}).fetchOne()
// . . .
0reactions
ricardogracacommented, Mar 4, 2018

Closing this since there are several proposed solutions and the user never responded back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL column reference "id" is ambiguous - Stack Overflow
This issue actually happens when there is same column name in both tables. where <tableName.columnName> = <value> can solve this issue. – Shah...
Read more >
How to Solve the “Ambiguous Name Column” Error in SQL
One of the simplest ways to solve an “ambiguous name column” error — without changing column name — is to give the tables...
Read more >
U122: Column reference is ambiguous - pganalyze
This error occurs when the referenced column can't be resolved unambiguously. This may occur when you have two tables that have columns with...
Read more >
Column is ambiguous SQL Error (Community) - Looker Support
You may see an error that says something like Column 'id' in field list is ambiguous . This error means that there is...
Read more >
What does the SQL 'ambiguous column name' error mean?
“Ambiguous column name” means that you are referencing an attribute or attributes that belong to more than one of the tables you are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found