“on子句”中的未知列“model1->model2.fieldname”

j1dl9f46  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(302)

我在这个错误中挣扎未知列错误。
我有三张table,下面是
市场——一对多关系(市场有许多产品。)
产品——一对一关系(产品属于市场)
国家——一对多关系(国家有许多产品。)
示例如下:

[{
    "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"
            }
        }
    ]
}]

如果am查询country表中的city和country name,则在sequelize中出现以下错误

{
"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;"
}

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']
})
ccrfmcuu

ccrfmcuu1#

改变

attributes: ['id', 'product_name', 'product_location', 'state_id', 'city'],

attributes: ['id', 'product_name', 'product_location', 'state_id', 'city','country_id'],

在产品模型属性中添加国家id

相关问题