【问题标题】:filter Sequelize belongsToMany过滤 Sequelize belongsToMany
【发布时间】:2019-12-14 00:03:57
【问题描述】:

我有以下问题:

我这样定义我的表(产品和集合):

module.exports = (sequelize, type) => {
    return sequelize.define('product', {
        id: {
            type: type.UUID,
            primaryKey: true,
            defaultValue: type.UUIDV4,
        },
        title: {
            type: type.STRING,
            allowNull: false,
        },
        description: {
            type: type.TEXT,
            allowNull: false,
        },
        width: {
            type: type.FLOAT,
            allowNull: false,
        },
        height: {
            type: type.FLOAT,
            allowNull: false,
        },
        weight: {
            type: type.FLOAT,
            allowNull: false,
        },
        length: {
            type: type.FLOAT,
            allowNull: false,
        },
        vendor: {
            type: type.STRING,
            allowNull: false,
        },
        status: {
            type: type.ENUM,
            values: ['inactive', 'active'],
            defaultValue: 'active',
            allowNull: false,
        },
    })
}
module.exports = (sequelize, type) => {
    return sequelize.define('collection', {
        id: {
            type: type.UUID,
            primaryKey: true,
            defaultValue: type.UUIDV4,
        },
        name: {
            type: type.STRING,
            allowNull: false,
        },
        image: {
            type: type.STRING,
            allowNull: true,
        },
        createdAt: {
            type: type.DATE,
            allowNull: false,
        },
        updatedAt: {
            type: type.DATE,
            allowNull: false,
        },
        status: {
            type: type.ENUM,
            values: ['inactive', 'active'],
            defaultValue: 'active',
            allowNull: false,
        },
    })
}

然后,我需要将表(产品和集合)与 belongsToMany 关联关联起来,我这样做了:

const ProductModel = require('../api/product/model')
const CategoryModel = require('../api/category/model')

const Product = ProductModel(sequelize, Sequelize)
const Collection = CollectionModel(sequelize, Sequelize)

Product.belongsToMany(Collection, {
    through: ProductCollection,
    foreignKey: 'productId',
    otherKey: 'collectionId',
    unique: false,        
})

Collection.belongsToMany(Product, {
    through: ProductCollection,
    foreignKey: 'collectionId',
    otherKey: 'productId',
    unique: false,        
})

现在,我想获取由请求正文发送的 id 给出的集合的所有产品,我没有时间使用 sequelize 并且我不知道如何进行此类查询。

你能帮我解决这个问题吗?

【问题讨论】:

    标签: node.js sequelize.js has-and-belongs-to-many findall


    【解决方案1】:

    你可以使用这样的东西

      let products = Collection.findAll({
                where: {
                    id: collection.id,
                },
                attributes: ['id', 'name'],
                include: [{
                    model: Product,
                    through: {
                        model: ProductCollection,
                    },
                    as: 'products',
                    attributes: ['id', 'title', 'description']
                }],
            });
    
            return products
    
            hope it helps
    

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2017-08-15
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2018-11-12
      相关资源
      最近更新 更多