query produces ambigious column reference when using attributes and include
See original GitHub issueThe 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:
- Created 8 years ago
- Reactions:5
- Comments:18 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had the same problem, and I could fix this easily by enabling the flag
subQuery
totrue
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.