获取关联/包含的表序列中的最小列

时间:2019-09-21 22:15:07

标签: node.js postgresql sequelize.js

我正在尝试获取产品编号,产品名称,产品可用性以及其变体的最低价格。

我尝试了什么? 我已经尝试了许多不同的代码变体,但是最接近解决方案的是使用以下代码:

product.findAll({
                attributes: [
                    'id',
                    'productName',
                    'productAvailability',
                    [
                        sequelize.fn('min', sequelize.col('variantPrice')),
                        'variantPrice'
                    ]
                ],
                include: [
                    {
                        model: variant,
                        attributes: [],
                        group : ['productId']
                    }
                ],
                where: {
                    businessId
                },
                group: [
                    'product.id', 'variants.id'
                ],
                order: [
                    ['updatedAt', 'DESC']
                ]
            }).map(el => el.get({plain: true}));

这段代码为我提供了我需要的值,但是由于某种原因,variantPrice不是最小值

我也尝试过以下查询的字面值,但这会返回带有提示的错误-也许您是想引用“ variant.variantPrice”列。

await sequelize.query(`SELECT product.*, MIN(variant.variantPrice) as variantPrice
            FROM product, variant
            WHERE businessId = :businessId
            AND product.id = variant.productId
            GROUP BY product.id
            ORDER BY product.updatedAt DESC`, {
                replacements : {
                    businessId
                }
            })

我的模特是:

产品

'use strict';
module.exports = (sequelize, DataTypes) => {
    const product = sequelize.define('product', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        productName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        productDescription: DataTypes.STRING,
        productAvailability: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true
        },
        productImage: DataTypes.STRING,
        businessId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'business',
                key: 'id'
            }
        }
    }, {});
    product.associate = function (models) {
        // associations can be defined here
        product.belongsTo(models.business);
        product.belongsToMany(models.category, {
            through: {
                model: models.productcategory
            }
        });
        product.belongsToMany(models.tag, {
            through: {
                model : models.producttag
            }
        });
        product.hasMany(models.variant);
    };
    return product;
};

变体

'use strict';
module.exports = (sequelize, DataTypes) => {
    const variant = sequelize.define('variant', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        variantName: {
            type: DataTypes.STRING,
            allowNull: false
        },
        variantPrice: {
            type: DataTypes.DECIMAL(14, 2),
            allowNull: false
        },
        variantSku: DataTypes.STRING,
        variantAvailability: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: true
        },
        productId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'product',
                key: 'id'
            }
        },
        position: DataTypes.INTEGER,
        variantType: DataTypes.STRING,
        variantImage: DataTypes.STRING
    }, {});
    variant.associate = function (models) {
        // associations can be defined here
        variant.belongsTo(models.product);
    };
    return variant;
};

0 个答案:

没有答案