使用Sequelize和Postgres,我正在尝试创建一个包含值及其单位的读数表。单位是另一个表单位的外键。我对协会感到困惑。
每个阅读必须具有太阳值/单位和月亮值/单位。 阅读单位可以不同
Readings
id | sun_temp | sun_temp_unit_id | moon_temp | moon_temp_unit_id
1 5 1 2 2
2 10 1 4 2
Units
id | Name
1 brapple
2 schmeckle
const Reading = sequelize.define("Reading", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
sun_temp: DataTypes.INTEGER,
moon_temp: DataTypes.INTEGER
}, {
timestamps: false,
tableName: "readings"
});
Reading.associate = (models) => {
Reading.belongsTo(models.Unit, {foreignKey: {name: 'sun_temp_unit_id'})
Reading.belongsTo(models.Unit, {foreignKey: {name: 'moon_temp_unit_id'})
}
const Unit = sequelize.define("Unit", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: DataTypes.STRING
}, {
timestamps: false,
tableName: "units"
});
Unit.associate = (models) => {
??
}