我在这个错误未知列错误中苦苦挣扎。
我有3张表,以下是
以下给出的示例JSON:
[{
"id": 1,
"name": "XYZ Name",
"code": "XYZCODE",
"Products": [
{
"id": 150,
"product_name": "ABC",
"product_location": 19,
"state_id": 24,
"city": "California",
"Country": {
"id": 19,
"name": "USA"
}
},
{
"id": 154,
"product_name": "DEF",
"product_location": 19,
"state_id": 24,
"city": "New York",
"Country": {
"id": 19,
"name": "USA"
}
}
]
}]
如果在国家/地区表中查询城市和国家/地区名称,则会在续集
中出现以下错误{
"code": "ER_BAD_FIELD_ERROR",
"errno": 1054,
"sqlState": "42S22",
"sqlMessage": "Unknown column 'Products->Country.name' in 'on clause'",
"sql": "SELECT `MarketplaceType`.`id`, `MarketplaceType`.`name`, `MarketplaceType`.`code`, `Products`.`id` AS `Products.id`, `Products`.`product_name` AS `Products.product_name`, `Products`.`product_location` AS `Products.product_location`, `Products`.`state_id` AS `Products.state_id`, `Products`.`city` AS `Products.city`, `Products->Country`.`id` AS `Products.Country.id`, `Products->Country`.`name` AS `Products.Country.name` FROM `marketplace_type` AS `MarketplaceType` LEFT OUTER JOIN `product` AS `Products` ON `MarketplaceType`.`id` = `Products`.`marketplace_type_id` AND `Products`.`status` = 1 AND `Products`.`marketplace_id` = 1 AND (`Products`.`city` = 'California' OR `Products->Country`.`name` = 'California') LEFT OUTER JOIN `country` AS `Products->Country` ON `Products`.`product_location` = `Products->Country`.`id` WHERE `MarketplaceType`.`status` = 1 AND `MarketplaceType`.`marketplace_id` = 1;"
}
AND MYSQL查询如下:
SELECT `MarketplaceType`.`id`, `MarketplaceType`.`name`, `MarketplaceType`.`code`, `Products`.`id` AS `Products.id`, `Products`.`product_name` AS `Products.product_name`, `Products`.`product_location` AS `Products.product_location`, `Products`.`state_id` AS `Products.state_id`, `Products`.`city` AS `Products.city`, `Products->Country`.`id` AS `Products.Country.id`, `Products->Country`.`name` AS `Products.Country.name` FROM `marketplace_type` AS `MarketplaceType` LEFT OUTER JOIN `product` AS `Products` ON `MarketplaceType`.`id` = `Products`.`marketplace_type_id` AND `Products`.`status` = 1 AND `Products`.`marketplace_id` = 1 AND (`Products`.`city` = 'California' OR `Products->Country`.`name` = 'California') LEFT OUTER JOIN `country` AS `Products->Country` ON `Products`.`product_location` = `Products->Country`.`id` WHERE `MarketplaceType`.`status` = 1 AND `MarketplaceType`.`marketplace_id` = 1;
使用sequelize进行ORM查询:
var marketplaceTypeQueryObj = {};
var productCountQueryParames = {};
marketplaceTypeQueryObj['status'] = status["ACTIVE"];
marketplaceTypeQueryObj['marketplace_id'] = marketplace['WHOLESALE'];
productCountQueryParames['status'] = status["ACTIVE"];
productCountQueryParames['marketplace_id'] = marketplace['WHOLESALE'];
if (req.query.origin) {
productCountQueryParames['$or'] = [{
city: req.query.origin
}, {
'$Products.Country.name$': req.query.origin
}, {
'$Products.State.name$': req.query.origin
}];
}
console.log('productCountQueryParames', productCountQueryParames);
model['MarketplaceType'].findAll({
where: marketplaceTypeQueryObj,
include: [{
model: model['Product'],
where: productCountQueryParames,
include: [{
model: model['Country'],
attributes: ['id', 'name']
}, {
model: model['State'],
attributes: ['id', 'name']
}],
attributes: ['id', 'product_name', 'product_location', 'state_id', 'city'],
required: false
}],
attributes: ['id', 'name', 'code']
})
答案 0 :(得分:0)
更改
attributes: ['id', 'product_name', 'product_location', 'state_id', 'city'],
到
attributes: ['id', 'product_name', 'product_location', 'state_id', 'city','country_id'],
在产品型号属性中添加国家/地区ID