What am i missing? any help is appreciated. Thanks
facility migration file
await queryInterface.createTable("facility", { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER, }, name: { allowNull: false, type: Sequelize.STRING(256), }, address: { allowNull: false, type: Sequelize.STRING(256), }, region_id: { type: Sequelize.BIGINT, references: { model: "region", key: "id", }, }, }
facility model file
`sequelize.define(
“facility”,
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
name: {
allowNull: false,
type: DataTypes.STRING(256),
},
address: {
allowNull: false,
type: DataTypes.STRING(256),
},
region_id: {
type: DataTypes.BIGINT,
}
})
Region.belongsTo(Facility, {
foreignKey: “region_id”,
});
Facility.hasOne(Region, {
foreignKey: “region_id”,
});
`
Executing (default): SELECT “facility”.”id”, “facility”.”name”, “facility”.”address”, “facility”.”region_id”, “region”.”id” AS “region.id”, “region”.”name” AS “region.name”, “region”.”region_id” AS “region.region_id” FROM “facility” AS “facility” LEFT OUTER JOIN “region” AS “region” ON “facility”.”id” = “region”.”region_id”;
column region.region_id does not exist error
I tried this association and it works, but I don’t know how it works.
Facility.belongsTo(Region, {
foreignKey: “region_id”,
});
Region.hasOne(Facility, {
foreignKey: “region_id”,
});
JPDR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.