[英]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.如您所见,此表中没有
id
列。 However when I try to insert it still tries the following sql:但是,当我尝试插入它时,它仍然会尝试以下 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?如何禁用它明确具有的
id
功能?
If you don't define a primaryKey
then sequelize uses id
by default.如果你没有定义一个
primaryKey
那么 sequelize 默认使用id
。
If you want to set your own, just use primaryKey: true
on 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
});
If you want to completely disable the primary key for the table, you can use Model.removeAttribute
.如果要完全禁用表的主键,可以使用
Model.removeAttribute
。 Be warned that this could cause problems in the future, as Sequelize is an ORM and joins will need extra setup.请注意,这可能会在未来导致问题,因为 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');
For a composite primary key, you should add "primaryKey: true" to all columns part of the primary key.对于复合主键,您应该将“primaryKey: true”添加到主键的所有列部分。 Sequelize considers such columns to be part of a composite primary key.
Sequelize 将此类列视为复合主键的一部分。
If your model does not have an ID column you can use Model.removeAttribute('id');如果您的模型没有 ID 列,您可以使用 Model.removeAttribute('id'); to get rid of it.
摆脱它。
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_id
and module_id
primary keys.考虑制作
academy_id
和module_id
主键。 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.这样同一个链接就不会出现多次,数据库也不会白白创建隐藏的id列。
In this solution, sequelize
adds default primary key with column name id.在这个解决方案中,
sequelize
添加了带有列名 id 的默认主键。 Add primaryKey: true
with your own defined primary key.使用您自己定义的主键添加
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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.