我有一个代码创建模型。但是当它运行时它会抛出错误
contactstatus.js:
module.exports = function(sequelize, DataTypes) {
var contact_status = sequelize.define('lk_contactstatus', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: false
},
name: DataTypes.STRING(32),
lk_recordstatusid: DataTypes.INTEGER
}, {
freezeTableName: true,
classMethods: {
associate: function (models) {
contact_status.belongsTo(
models.r_status,
{foreignKey: 'lk_recordstatusid'}
);
}
}
});
return contact_status;
};
recordstatus.js
module.exports = function(sequelize, DataTypes) {
var r_status = sequelize.define('lk_record_status', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: false
},
name: DataTypes.STRING(16)
}, {
freezeTableName: true
});
return r_status;
};
用一些不是Sequelize.Model实例的东西来调用 null.belongsTo(F:\ CONG VIEC \ Auvene \ sampleczarapi \ node_modules \ sequelize \ lib中\协会\ mixin.js:95:13) at sequelize.define.classMethods.associate(F:\ CONG VIEC \ Auvene \ sampleczarapi \ models \ facp-contact \ contact_type.js:10:22)
答案 0 :(得分:0)
您正在使用不是模型的东西来调用belongsTo(使用不是Sequelize.Model 的实例调用的东西)。
您将模型名称定义为 lk_record_status ,但之后将其称为 r_status 。
module.exports = function(sequelize, DataTypes) {
var contact_status = sequelize.define('lk_contactstatus', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: false
},
name: DataTypes.STRING(32),
lk_recordstatusid: DataTypes.INTEGER
}, {
freezeTableName: true,
classMethods: {
associate: function (models) {
contact_status.belongsTo(
models.lk_record_status,
{foreignKey: 'lk_recordstatusid'}
);
}
}
});
return contact_status;
};