gpt4 book ai didi

sequelize.js - Sequelize 生成出错的 sql 查询

转载 作者:行者123 更新时间:2023-12-03 22:37:16 25 4
gpt4 key购买 nike

我收到以下错误。下面给出了生成的查询。

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

我的所有表列看起来都像蛇皮案。
表模型声明如下。

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

最佳答案

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

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

关于sequelize.js - Sequelize 生成出错的 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57699469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com