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.

Model definition without primaryKey while overwriting the id property

See original GitHub issue

When 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:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
sfertmancommented, Mar 11, 2021

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.

3reactions
vikram-chaitanyacommented, Sep 5, 2015

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?

  var Project = sequelize.define('Project', {
    id:{
      type: Sequelize.BIGINT(20),
      allowNull: false,
      autoIncrement: true
    },
    title: Sequelize.STRING,
    description: Sequelize.TEXT,
    account_id: {
      type: Sequelize.BIGINT(20),
      allowNull: false
    }
  },
  {
    indexes: [
      {
        name: 'id_and_account_id',
        type: 'UNIQUE',
        fields: ['id', 'account_id']
      }
    ]
  });
Read more comments on GitHub >

github_iconTop 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 >

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