代码之家  ›  专栏  ›  技术社区  ›  PhilippeAuriach

对hasMany计数为空的模型进行顺序查询

  •  1
  • PhilippeAuriach  · 技术社区  · 10 年前

    我有一个产品模型,它链接到具有hasMany关系的媒体表:

    Product.hasMany(models.Media, {
        foreignKey: 'mediableId',
        constraints: false,
        scope: {
            mediable: 'product'
        },
        as: 'medias'
    });
    

    我正在寻找一种查询所有零媒体产品的方法,如何使用Sequelize?如果没有原始查询,是否可能?

    1 回复  |  直到 10 年前
        1
  •  1
  •   user5383152 user5383152    10 年前

    您可以对where子句使用原始查询。

    例如,假设 Product 模型定义如下:

    module.exports = (sequelize, DataTypes) => sequelize.define('products', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: true
        }
    }, {
        tableName: 'products',
        freezeTableName: true,
        underscored: true,
        classMethods: {
            associate: models => {
                models.products.hasMany(models.medias, {
                    foreignKey: 'mediable_id',
                    constraints: false,
                    scope: {
                        mediable: 'product'
                    }
                });
            }
        }
    });
    

    和你的 Media 模型定义为:

    module.exports = (sequelize, DataTypes) => sequelize.define('medias', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            allowNull: true
        },
        mediable: {
            type: DataTypes.STRING,
            allowNull: true
        },
        mediable_id: {
            type: DataTypes.INTEGER,
            allowNull: true
        }
    }, {
        tableName: 'medias',
        freezeTableName: true,
        underscored: true,
        classMethods: {
            associate: models => {
                models.products.belongsTo(models.products, {
                    foreignKey: 'id',
                    constraints: false
                });
            }
        }
    });
    

    然后你可以这样查询:

    sequelize.sync().then(() => Promise.all([
    
        // Prepopulate Data
        models.products.upsert({
            id: 1,
            name: 'Product A'
        }),
        models.products.upsert({
            id: 2,
            name: 'Product B'
        }),
        models.products.upsert({
            id: 3,
            name: 'Product C'
        }),
    
        models.medias.upsert({
            id: 1,
            name: 'Media A',
            mediable: 'item',
            mediable_id: 1
        }),
        models.medias.upsert({
            id: 2,
            name: 'Media B',
            mediable: 'product',
            mediable_id: 1
        }),
        models.medias.upsert({
            id: 3,
            name: 'Media C',
            mediable: 'product',
            mediable_id: 1
        }),
        models.medias.upsert({
            id: 4,
            name: 'Media D',
            mediable: 'product',
            mediable_id: 2
        }),
        models.medias.upsert({
            id: 5,
            name: 'Media E'
        })
    
    ])).then(() => models.products.findAll({
        where: ["medias.mediable_id IS NULL OR medias.mediable != 'product'"],
        include: [{
            model: models.medias,
            required: false,
        }]
    })).then(products => {
        // The rest of your logic here...
    });
    
    推荐文章