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.

Stop providing a default `id` attribute for models

See original GitHub issue

The default id that Sequelize provides for models is often source of confusion.

const Foo = sequelize.define('foo', {
  name: DataTypes.STRING
});

Is actually equivalent to

const Foo = sequelize.define('foo', {
  id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true
  },
  name: DataTypes.STRING
});

It has the obvious advantage of less boilerplate code. However, it is often source of confusion and weird errors, such as in issue #11723 (as clarified in this comment) and #11714

I suggest a breaking change of no longer providing this default attribute. Let’s make users define them. This way this kind of confusion will never happen.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
soryy708commented, May 21, 2020

The feature of auto-generating the id column is indeed useful for minimizing boilerplate, but it does cause various problems. The type of problems caused touch on many sequelize subsystems and tend to be complicated edge cases. Fixing all of the issues caused by the auto-generated id column may require a lot of effort. Removing the id column auto-generation would mean a breaking API change, but the effort required for migration is very small (and perhaps can be automated with a tool). This issue was open since 2016, and it looks like no one’s in opposition to the removal.

1reaction
soryy708commented, Jul 3, 2020

That’s a good idea @Keimeno . To maintain backwards compatibility, generatePrimaryKey will be true by default (current behaviour). We can then print a warning to the log if generatePrimaryKey isn’t explicitly present, s.t. a version later we can change the API to make it false by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'id' attribute is always selected when performing model.findAll ...
Successful query generated, i.e.. SELECT "ObjectId", "createdAt", "updatedAt", "StaffId", "StaffRoleId" FROM "model" AS "SRL" WHERE " ...
Read more >
sequelize table without column 'id' - Stack Overflow
If you don't define a primaryKey then sequelize uses id by default. If you want to set your own, just use primaryKey: true...
Read more >
Automatic IDs, None Defaults, and Refreshing Data - SQLModel
Because we don't set the id , it takes the Python's default value of None that we set in Field(default=None) . This is...
Read more >
Attributes - Sails.js
For example, new Sails apps come with three default attributes out of the box: id , createdAt , and updatedAt . These attributes...
Read more >
Creating a not-empty GUID validation attribute and a not ...
In this post I described a [NotEmpty] validation attribute that can be used to detect when a Guid property on a model is...
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