0

I am getting the following error. The generated query is given below.

Error Code: 1054. Unknown column 'country.countryId' in 'field list'    0.0054 sec

My all table columns look likes snake_case. Table model declaration is given below.

export default function (sequelize: Sequelize): TModel<i.StateAttributes> {
    const state = sequelize.define('state', {
       id: { type: DataTypes.BIGINT, field: 'state_id', primaryKey: true, autoIncrement: true },
       organizationId: { type: DataTypes.BIGINT, field: 'organization_id' },
       facilityId: { type: DataTypes.BIGINT, field: 'facility_id' },
       countryId: { type: DataTypes.BIGINT, field: 'country_id' },
       stateCode: { type: DataTypes.STRING, field: 'state_code' },
       stateName: { type: DataTypes.STRING, field: 'state_name' },
       isActive: { type: DataTypes.BOOLEAN, field: 'is_active' },
       activeStatusId: { type: DataTypes.INTEGER, field: 'active_status_id' },
       status: { type: DataTypes.INTEGER, field: 'status' },
       rev: { type: DataTypes.INTEGER, field: 'rev' },
       createdBy: { type: DataTypes.INTEGER, field: 'created_by' },
       createdAt: { type: DataTypes.DATE, field: 'created_at' },
       updatedBy: { type: DataTypes.INTEGER, field: 'updated_by' },
       updatedAt: { type: DataTypes.DATE, field: 'updated_at' },
    },
        {
            indexes: [],
            underscored: true,
            timestamps: true,
            tableName: 'state',
            createdAt: 'created_at',
            updatedAt: 'updated_at',
            freezeTableName: true,
            defaultScope: {
                where: {
                    status: 1,
                },
            },
        }) as TModel<i.StateAttributes>;
    return state;
}

state.belongsTo(models.country);

generated query is given below. country.countryId AS country.countryId, lines comes extra which throws error. the property does not have snake_case.

SELECT 
    `state`.`state_id` AS `id`,
    `state`.`state_name` AS `stateName`,
    `country`.`country_id` AS `country.id`,
    `country`.`countryId` AS `country.countryId`,
    `country`.`organization_id` AS `country.organizationId`,
    `country`.`facility_id` AS `country.facilityId`,
    `country`.`country_code` AS `country.countryCode`,
    `country`.`country_name` AS `country.countryName`,
    `country`.`is_active` AS `country.isActive`,
    `country`.`active_status_id` AS `country.activeStatusId`,
    `country`.`status` AS `country.status`,
    `country`.`rev` AS `country.rev`,
    `country`.`created_by` AS `country.createdBy`,
    `country`.`created_at` AS `country.createdAt`,
    `country`.`updated_by` AS `country.updatedBy`,
    `country`.`updated_at` AS `country.updatedAt`
FROM
    `state` AS `state`
        LEFT OUTER JOIN
    `country` AS `country` ON `state`.`country_id` = `country`.`country_id`
        AND `country`.`status` = 1
WHERE
    `state`.`status` = 1
LIMIT 0 , 50;
`
Natarajan Ganapathi
  • 551
  • 1
  • 7
  • 19

1 Answers1

0

Problems of this type can usually be solved by making the association a little more specific. Making some assumptions about the state model... this might solve:

state.belongsTo(models.country, {foreignKey: "countryId", targetKey: "id"} );
KenOn10
  • 1,743
  • 1
  • 9
  • 10