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

곽대용*_*곽대용 6 orm join include node.js sequelize.js

我想从关注模型中获取限制为 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
    }
}
Run Code Online (Sandbox Code Playgroud)

并且,加入

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'})
Run Code Online (Sandbox Code Playgroud)

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

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'})
Run Code Online (Sandbox Code Playgroud)

但我想在限制 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
                        }
                    ]
                }
            ]
        })
Run Code Online (Sandbox Code Playgroud)

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

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

都一样..

问题是什么?

我怎么解决这个问题??

Viv*_*shi 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
    }
]
Run Code Online (Sandbox Code Playgroud)

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

  • 如果运行“where”子句,则无法应用“separate: true” (2认同)