Javascript 没有列“id”的续集表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29233896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:01:44  来源:igfitidea点击:

sequelize table without column 'id'

javascriptmysqlnode.jssequelize.js

提问by Marc Rasmussen

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 idcolumn in this table. However when i try to insert it still tries the following sql:

如您所见id,此表中没有列。但是,当我尝试插入它时,它仍然会尝试以下 sql:

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

How can i disable the idfunction it clearly has?

如何禁用id它明显具有的功能?

回答by Ben Fortune

If you don't define a primaryKeythen sequelize uses idby default.

如果您没有定义,primaryKey则 sequelizeid默认使用。

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

如果您想设置自己的,只需primaryKey: true在您的列上使用。

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

回答by Ben Fortune

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.

如果要完全禁用表的主键,可以使用Model.removeAttribute. 请注意,这可能会在未来导致问题,因为 Sequelize 是一个 ORM,连接需要额外设置。

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

回答by gaurav

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.

对于复合主键,您应该将“primaryKey: true”添加到主键的所有列部分。Sequelize 将此类列视为复合主键的一部分。

回答by Andreas

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

如果您的模型没有 ID 列,您可以使用 Model.removeAttribute('id'); 摆脱它。

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

请参阅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_idandmodule_idprimary 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.

注意:您似乎正在制作连接表。考虑制作academy_idmodule_id主键。这样同一个链接就不会出现多次,数据库也不会白白创建隐藏的id列。