sequelize / sequelize/sequelize
Duplicate column name 'Id'
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
Issue Description
What are you doing?
I am having an issue when I try to use eager loading (joined queries) via Sequelize. Here are the details:
ProductGroup model definition
...
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
field: 'Id'
},
name: {
type: DataTypes.STRING(400),
allowNull: false,
field: 'Name'
}
...
...
// association
ProductGroup.hasMany(
ProductScreenshot,
{ as: 'screenshots', foreignKey: 'productGroupId' }
)
ProductScreenshot model definition
...
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
field: 'Id'
},
productGroupId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'ProductGroupId'
}
...
...
// association
ProductScreenshot.belongsTo(
ProductGroup,
{ foreignKey: 'productGroupId' }
)
And below query throws Duplicate column name 'Id' error when I try to get ProductScreenshot rows belongs to the queried ProductGroup.
query = Object.assign({}, {
where: {
id: {
[Sequelize.Op.eq]: 1
}
},
include: [
{
as: 'screenshots',
model: 'ProductScreenshot'
}
]
})
// run the query
const gameGroup = await db.models.ProductGroup.findOne({
...query,
tableHint: Sequelize.TableHints.NOLOCK
})
What do you expect to happen?
There should not be any duplication in the generated SQL query.
What is actually happening?
Check the generated SQL example below that throws the error for more information.
SELECT `ProductGroup`.*,
`screenshots`.`id` AS `screenshots.id`,
`screenshots`.`name` AS `screenshots.name`
FROM (SELECT `productgroup`.`id` AS `id`, /* duplication */
`productgroup`.`name` AS `name`,
`productgroup`.`id` /* duplication */
FROM `productgroup` AS `ProductGroup`
WHERE `productgroup`.`id` = 1
LIMIT 1) AS `ProductGroup`
LEFT OUTER JOIN `screenshot` AS `screenshots`
ON `ProductGroup`.`id` = `screenshots`.`productgroupid`
Environment
- Sequelize version: 6.0.0-beta.6
- Node.js version: v12.18.0
- Operating System: Windows 10
- Dialect: mariadb
- DB: MySQL
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ProductGroup and ProductScreenshot model definitions, associations, and the ProductGroup.findOne query shown in the issue. Reproduce the generated SQL with the MariaDB/MySQL setup and trace how the eager-loading query is assembled. Done means the query no longer selects the duplicate Id column and returns the related screenshots successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mariadb, mysql, node.js
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100