0

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", }); 
1
  • The second one is correct because we call belongsTo from the model that has a FK column Commented Sep 23, 2024 at 16:20

1 Answer 1

0
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"."region_id" = "region"."id"; 

as given your relation between table your select query need to change like this need to change

ON "facility"."id" = "region"."region_id"; 

to ON "facility"."region_id" = "region"."id"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.