我如何在使用sequelize的包含模型中使用限制

问题描述 投票:0回答:2

我想从关注模型中获取限制为2的用户图像。

型号

const Follow = connector.define('Follow', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    follower_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    },
    target_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    delete_dt
}

const User = connector.define('User', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: {
        type: Sequelize.STRING,
        allowNull: false

    },
    email: {
        type: Sequelize.STRING,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    profile_img: {
        type: Sequelize.STRING,
        allowNull: true
    },
    bio: {
        type: Sequelize.STRING,
        allowNull: true
    },
    phone: {
        type: Sequelize.STRING,
        allowNull: true
    },
    gender: {
        type: Sequelize.STRING,
        allowNull: true
    },
    website: {
        type: Sequelize.STRING,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    update_dt,
    delete_dt
}

const Image = connector.define('Image', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    file: {
        type: Sequelize.STRING,
        allowNull: false
    },
    location: {
        type: Sequelize.STRING,
        allowNull: true
    },
    caption: {
        type: Sequelize.STRING,
        allowNull: true
    },
    tags: {
        type: Sequelize.STRING,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    update_dt,
    delete_dt,
    user_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    }
}

并且加入

User.hasMany(Image, {foreignKey: 'user_id'})
Image.belongsTo(User, {foreignKey: 'user_id'})

User.hasMany(Follow, {foreignKey: 'follower_id'})
Follow.belongsTo(User, {foreignKey: 'follower_id'})

User.hasMany(Follow, {foreignKey: 'target_id'})
Follow.belongsTo(User, {foreignKey: 'target_id'})

所以,我尝试通过使用 include 来获取用户的图像。

const followerImages = await Follow.findAll({
            attributes: ['target_id'],
            where:{
                follower_id: loginUser_id
            },
            include:[
                {
                    model: User,
                    required: true,
                    attributes: ['username', 'email', 'profile_img'],
                    include:[
                        {
                            model: Image,
                            required: true
                        }
                    ]
                }
            ]
        })

但我想获取限制为 2 的图像。

所以我尝试了

const followerImages = await Follow.findAll({
            attributes: ['target_id'],
            where:{
                follower_id: loginUser_id
            },
            include:[
                {
                    model: User,
                    required: true,
                    attributes: ['username', 'email', 'profile_img'],
                    include:[
                        {
                            model: Image,
                            required: true,
                            limit: 2
                        }
                    ]
                }
            ]
        })

但它会产生我无法理解的错误。

images 字段是一个包含 4 处空对象的数组。

都一样..

有什么问题吗?

我该如何解决这个问题??

node.js join orm include sequelize.js
2个回答
7
投票

你可以尝试一下:

include:[
    {
        model: Image,
        attributes : ['id','user_id','image'] , // <---- don't forget to add foreign key ( user_id )
        separate : true, // <--- Run separate query
        limit: 2
    }
]

Limit
有时会在嵌套级别上导致问题,因此单独运行该查询总是安全的。


0
投票

也许你可以应用这个解决方案。我尝试通过在sequelize中添加一个具有错误值的字段来关闭复制和子查询,如下所示:

duplicating: false, 
subQuery: false

例如如下:

const followerImages = await Follow.findAll({
            attributes: ['target_id'],
            where:{
                follower_id: loginUser_id
            },
            include:[
                {
                    model: User,
                    required: true,
                    attributes: ['username', 'email', 'profile_img'],
                    include:[
                        {
                            model: Image,
                            required: true
                        }
                    ]
                }
            ],
            duplicating: false,
            subQuery: false
        })

参考:https://github.com/sequelize/sequelize/issues/3007

© www.soinside.com 2019 - 2024. All rights reserved.