【问题标题】:Sequelize generates sql query which is getting errorSequelize 生成出现错误的 sql 查询
【发布时间】:2019-08-29 23:06:18
【问题描述】:

我收到以下错误。生成的查询如下所示。

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

我的所有表格列看起来都像snake_case。 表模型声明如下。

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);

生成的查询如下所示。 country.countryId AS country.countryId, 额外的行会引发错误。该属性没有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;
`

【问题讨论】:

    标签: sequelize.js sequelize-cli sequelize-typescript


    【解决方案1】:

    这种类型的问题通常可以通过使关联更具体一些来解决。对状态模型做一些假设......这可能会解决:

    state.belongsTo(models.country, {foreignKey: "countryId", targetKey: "id"} );
    

    【讨论】:

    • 感谢您的宝贵时间。它不适合我。我们收到以下错误Unknown column 'country.countryId' in 'field list'
    • 请贴出国家型号
    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 2018-07-23
    • 2015-12-13
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多