Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize table without column 'id'

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?

like image 592
Marc Rasmussen Avatar asked Mar 24 '15 13:03

Marc Rasmussen


People also ask

How do I remove a Sequelize ID?

INTEGER }, { timestamps: false } ); By default, Sequelize will assume the table uses an id column as its primary key. Now you can call destroy() from the Invoice model to delete a row by its id value: const count = await Invoice.

Does Sequelize automatically add an ID?

Sequelize will assume your table has a id primary key property by default.

How do you prevent createdAt and updatedAt in Sequelize?

With timestamps: false , the generated model will omit the createdAt and updatedAt attributes. You can also opt to include only the timestamp attribute you need as shown below: const User = sequelize. define( "User", { firstName: Sequelize.


1 Answers

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 }); 
like image 76
Ben Fortune Avatar answered Sep 22 '22 03:09

Ben Fortune