Reputation: 798
I have a node project using sequelize, and have the following two tables
"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;
};
The problem is that when i check the asset tables in postgres I have two columns ("assetState_id", "assetState") I suppose that is because i give the name of the relation explicitly, as I can do to not generate the column "assetState"?
thanks
Upvotes: 1
Views: 4708
Reputation: 1
I have found that if you put the tableName in lowercase, the foreign keys will not being duplicated any more
dialect: 'mysql'
Upvotes: 0
Reputation: 9438
You can declare a primary key in the assetState table and then reference that in the association definition:
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' })
}
}
});
Upvotes: 1