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.

query produces ambigious column reference when using attributes and include

See original GitHub issue

The following example fails with

Unhandled rejection SequelizeDatabaseError: column reference "name" is ambiguous
var S = require('sequelize');

var sequelize = new S('seq_test', 'postgres', '', {
//var sequelize = new S('seq_test', 'passwd', '', {
    host: 'localhost',
    dialect: 'postgres',
    pool: { max: 15, min: 0, idle: 10000, },
    define: {  },
});

var User = sequelize.define('User', {
    name: { type: S.STRING(50), allowNull: true},
    pass: { type: S.STRING(50), allowNull: true},
});
var Foo = sequelize.define('Foo', {
    name: { type: S.STRING(50), allowNull: true},
    pass: { type: S.STRING(50), allowNull: true},
});

User.belongsTo(Foo, {});

sequelize.sync()
.then(function (){
    User.findAll({
        //raw: true,
        attributes: [  [ S.col('name'), 'username' ]   ],
        include: [
            {   model: Foo,
                attributes: [ [ S.col('name'), 'fooname' ] ],
            },
        ],
    });
});

sql produced.

SELECT 
  "name" AS "username", 
  "Foo"."name" AS "Foo.fooname" 
FROM "Users" AS "User" 
LEFT OUTER JOIN "Foos" AS "Foo" ON "User"."FooId" = "Foo"."id";

as you can see the S.col(‘name’) in the User relation did not prefix the column with the table alias.

If there is no “attributes” section, and all columns are used then columns from the user relation are prefixed.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:5
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
diegohidekycommented, Mar 31, 2020

I had the same problem, and I could fix this easily by enabling the flag subQuery to true

User.findAll({
        //raw: true,
        attributes: [  [ S.col('name'), 'username' ]   ],
        include: [
            {   model: Foo,
                attributes: [ [ S.col('name'), 'fooname' ] ],
            },
        ],
        subQuery: true, <-------------------------------
    });
3reactions
je-poycommented, Aug 14, 2019

Try to add the table name on your Sequelize.col and use it on its singular form. Also try to add “->” if it is nested.

sequelize.sync().then(function() {
    User.findAll({
        attributes: [[ Sequelize.col('User.name'), 'username' ]],
        include: {
            model: Foo,
            attributes: [[ Sequelize.col('User->Foo.name'), 'fooname' ]],
        }
    });
});```
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 >
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 >
SQL Query error: ambiguous column name geometry
I have tried with .geometry in the ST_Intersects and this gives correct counts but gives the pragma geometry error when the sum is...
Read more >
Documentation: 15: 7.2. Table Expressions - PostgreSQL
However, the reference produces only the columns that appear in the named ... If the tables have N and M rows respectively, the...
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