sequelize table without column 'id'

JavascriptMysqlnode.jssequelize.js

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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
});

Solution 2 - Javascript

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');

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

In this solution, sequelize adds default primary key with column name id. Add primaryKey: true with your own defined primary key.

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
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarc RasmussenView Question on Stackoverflow
Solution 1 - JavascriptBen FortuneView Answer on Stackoverflow
Solution 2 - JavascriptBen FortuneView Answer on Stackoverflow
Solution 3 - JavascriptgauravView Answer on Stackoverflow
Solution 4 - JavascriptAndreasView Answer on Stackoverflow
Solution 5 - JavascriptsamranView Answer on Stackoverflow