Stop providing a default `id` attribute for models
See original GitHub issueThe 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:
- Created 4 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top 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 >
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
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-generatedid
column may require a lot of effort. Removing theid
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.That’s a good idea @Keimeno . To maintain backwards compatibility,
generatePrimaryKey
will betrue
by default (current behaviour). We can then print a warning to the log ifgeneratePrimaryKey
isn’t explicitly present, s.t. a version later we can change the API to make itfalse
by default.