0
我是新來的續集。我已經實現了setter和getters到我的模型,然後我添加了一個外鍵,但它沒有創建。只有當我移除getters和setter時纔會創建它。 我做錯了什麼?爲什麼不使用getter和setter創建外鍵?
示範區
module.exports = function(sequelize, DataTypes) {
const Zone = sequelize.define('Zone', {
zone: {
type: DataTypes.STRING,
allowNull: false
}
},{
getterMethods: {
zone:() => {
return this.getDataValue('zone');
}
},
setterMethods: {
zone: (zone) => {
return this.setDataValue('zone', zone);
}
}
},{
classMethods: {
associate: (models) => {
Zone.hasMany(models.Client, {
foreignKey: 'zone_id',
as: 'zoneFk'
});
},
},
});
return Zone;
};
模型客戶
module.exports = (sequelize, DataTypes) => {
const Client = sequelize.define('Client',{
//atributtes...
}, {
classMethods: {
associate: (models) => {
Client.belongsTo(models.Zone, {
foreignKey: 'zone_id',
onDelete: 'CASCADE'
});
},
},
});
return Client;
};