sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Duplicate column name 'Id'

Open hsynlms opened this issue 6 years ago • 4 comments

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

hsynlms avatar Jun 20 '20 17:06 hsynlms

Here is my test result: when I delete identity column in definitions it seems working.

// removed identity column
id: {
  type: DataTypes.INTEGER,
  autoIncrement: true,
  primaryKey: true,
  field: 'Id'
}

hsynlms avatar Jun 20 '20 18:06 hsynlms

I am having this exact same issue

toriaezunama avatar Sep 21 '20 18:09 toriaezunama

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

github-actions[bot] avatar Nov 02 '21 08:11 github-actions[bot]

I'm also having this issue

Knguyen21 avatar Nov 30 '23 18:11 Knguyen21