admin管理员组文章数量:1134569
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?
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?
5 Answers
Reset to default 174If 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
});
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');
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.
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.
In case if you are using sequelize cli to generate models, removeAttribute to be written like below
module.exports = (sequelize, DataTypes) => {
class follow extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
follow.init({
follower_id: DataTypes.INTEGER,
followee_id: DataTypes.INTEGER
}, {
sequelize,
modelName: 'follows',
});
follow.removeAttribute("id");
return follow;
};
本文标签: javascriptsequelize table without column 39id39Stack Overflow
版权声明:本文标题:javascript - sequelize table without column 'id' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736823439a1954387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论