Model definition without primaryKey while overwriting the id property
See original GitHub issueWhen you create two models and one does overwrite the id property and didn’t set the primaryKey, you are running into interesting problems when association both models.
var User = sequelize.define('User', { username: Sequelize.STRING, space_id: Sequelize.STRING }, { tableName: 'users', underscored: true })
var Space = sequelize.define('Space', { id: Sequelize.STRING }, { tableName: 'spaces', underscored: true })
User.belongsTo(Space)
TypeError: Cannot read property 'type' of undefined
at BelongsTo.injectAttributes (.../node_modules/sequelize/lib/associations/belongs-to.js:119:153)
at null.belongsTo (.../node_modules/sequelize/lib/associations/mixin.js:106:77)
at repl:1:7
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:203:10)
at Interface._line (readline.js:532:8)
at Interface._ttyWrite (readline.js:761:14)
at ReadStream.onkeypress (readline.js:100:10)
at ReadStream.emit (events.js:98:17)
at emitKey (readline.js:1096:12)
at ReadStream.onData (readline.js:841:14)
at ReadStream.emit (events.js:95:17)
at ReadStream.<anonymous> (_stream_readable.js:765:14)
at ReadStream.emit (events.js:92:17)
This problem does not occur anymore, when the space’s id property is marked as primaryKey: true
.
So in case a model does not have a primaryKey property and the default primary key property (id) is overwritten, we should print a warning somewhere.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
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 >Models - Django documentation
Models ¶. A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the...
Read more >Keys - EF Core | Microsoft Learn
In this article. Configuring a primary key; Value generation; Primary key name; Key types and values; Alternate Keys.
Read more >What Is MongoDB's _id Field and How to Use It - Orange Matter
That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID. Overriding the...
Read more >Chapter 2. Mapping Entities - Red Hat on GitHub
You can also override the access type of a single property while keeping the ... An embedded id can itself contains the primary...
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
This makes no sense. If I have a column named
id
, it doesn’t have to be the primary key! Just because “something is fishy” doesn’t mean that you have to break the world over it.Hi,
In mysql we are partitioning a table which may not have a primary key or has composite primary key. This fix might break those model definitions. I have table with id and account_id, which is partitioned by account_id, so my primary key would be (id, account_id), but with this fix the table definition is not possible. In the below table since i could not define a composite primary key i just defined the same as unique index.
Is there some other workaround?