我有一个使用sequelize的节点项目,并有以下两个表
"use strict";
module.exports = function(sequelize, DataTypes) {
var asset = sequelize.define("asset", {
description : DataTypes.STRING,
maker : DataTypes.STRING,
model : DataTypes.STRING,
serialNumber : DataTypes.STRING,
barcode : DataTypes.STRING,
account : DataTypes.INTEGER,
usefulLife : DataTypes.INTEGER,
downtimeCosts : DataTypes.FLOAT,
purchasePrice : DataTypes.FLOAT
}, {
classMethods: {
associate: function(models) {
/*Un estado es de muchas categorias, y no se puede borrar si tiene un activo asociado*/
asset.belongsTo(models.assetState, {
onDelete: "RESTRICT",
foreignKey: {
allowNull : false,
name : 'assetState_id'
}
});
}
}
});
return asset;
};
"use strict";
module.exports = function(sequelize, DataTypes) {
var assetState = sequelize.define("assetState", {
state: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
assetState.hasMany(models.asset)
}
}
});
return assetState;
};
问题是,当我检查postgres中的资产表时,我有两列(“assetState_id”,“assetState”)我想这是因为我明确地给出了关系的名称,因为我可以做到不生成列“assetState”?
感谢
答案 0 :(得分:1)
您可以在assetState表中声明主键,然后在关联定义中引用它:
var assetState = sequelize.define("assetState", {
assetState_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
state: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
assetState.hasMany(models.asset, { foreignKey: 'assetState_id' })
}
}
});