112

I have the following sequelize definition of a table:

AcademyModule = sequelize.define('academy_module', {
        academy_id: DataTypes.INTEGER,
        module_id: DataTypes.INTEGER,
        module_module_type_id: DataTypes.INTEGER,
        sort_number: DataTypes.INTEGER,
        requirements_id: DataTypes.INTEGER
    }, {
        freezeTableName: true
});

As you can see there is not an id column in this table. However when I try to insert it still tries the following sql:

 INSERT INTO `academy_module` (`id`,`academy_id`,`module_id`,`sort_number`) VALUES (DEFAULT,'3',5,1);

How can I disable the id function it clearly has?

Vladislav Povorozniuc
  • 2,149
  • 25
  • 26
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

4 Answers4

166

If you don't define a primaryKey then sequelize uses id by default.

If you want to set your own, just use primaryKey: true on your column.

AcademyModule = sequelize.define('academy_module', {
    academy_id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER
}, {
    freezeTableName: true
});
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
  • 3
    What if both module_id and academy_id is a primaryKey? – Marc Rasmussen Mar 24 '15 at 13:50
  • 1
    A table can only have one primary key, unless you create a compound primary key. I'm not sure if Sequelize supports this. – Ben Fortune Mar 24 '15 at 13:51
  • 22
    Disregard the above, Sequelize does support this. Just add `primaryKey: true` to the columns you want to use as a primary key. – Ben Fortune Mar 24 '15 at 13:56
  • how to delare primary key using sequelize-cli like something like this sequelize model:create --name MyUsera --attributes first_name:Integer:primaryKey: true,last_name:string,bio:text – Rizwan Patel May 13 '16 at 07:35
  • 12
    Why is this even regarded as an answer? The question was very clearly "How can i disable the `id` function it clearly has?" and this answer tries to answer it with "If you want you can change the name of the primary key". What?? – Andreas Aug 10 '16 at 10:48
  • Maybe you should read the answer as: 'Your tables MUST have a primary key, so it cannot be disabled, but you can change the name/definition of it'. Not sure whether it's actually impossible to completely disable it though. – Stijn de Witt Oct 10 '16 at 12:03
  • 12
    According to [sequelize configurtion](http://docs.sequelizejs.com/en/latest/docs/models-definition/#configuration), one can define which of the existing/defined fields will be the `id` (you can set multiple filds as `primaryKey: true`) and if you have just no id on your table, then [sequelize legazy](http://docs.sequelizejs.com/en/latest/docs/legacy/) documentation says you can just remove it with `Model.removeAttribute('id');` and you will have a table (model) without primary key. – Aaron C Oct 28 '16 at 20:47
63

If you want to completely disable the primary key for the table, you can use Model.removeAttribute. Be warned that this could cause problems in the future, as Sequelize is an ORM and joins will need extra setup.

const AcademyModule = sequelize.define('academy_module', {
    academy_id: DataTypes.INTEGER,
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER
}, {
    freezeTableName: true
});
AcademyModule.removeAttribute('id');
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
25

If your model does not have an ID column you can use Model.removeAttribute('id'); to get rid of it.

See http://docs.sequelizejs.com/en/latest/docs/legacy/

So in your case it would be

AcademyModule = sequelize.define('academy_module', {
    academy_id: DataTypes.INTEGER,
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER
}, {
    freezeTableName: true
});
AcademyModule.removeAttribute('id');

Note: You seem to be making a join table. Consider making academy_id and module_id primary keys. That way the same link will not be able to appear multiple times, and the database will not create a hidden id column in vain.

Andreas
  • 662
  • 1
  • 6
  • 13
20

For a composite primary key, you should add "primaryKey: true" to all columns part of the primary key. Sequelize considers such columns to be part of a composite primary key.

gaurav
  • 360
  • 3
  • 8
  • how to delare custome primary key using sequelize-cli like something like this sequelize model:create --name MyUsera --attributes first_name:Integer:primaryKey: true,last_name:string,bio:text – Rizwan Patel May 13 '16 at 07:36
  • This was back in 2015 and I can't figure out what is going on. How do I build the relationships with a compound key?? – Phil Apr 03 '18 at 18:51
  • OP has asked for "How can I disable the id function it clearly has?" this is clearly the wrong answer. – Overbryd Apr 09 '21 at 07:33
  • You're right. OP would have marked this as an answer if this was the direct unblocking solution. This extends the right answer for cases where the primary key is composite. You clearly have not read the solution Ben Fortune provided. – gaurav Apr 11 '21 at 00:14