Search code examples
node.jsjoinormincludesequelize.js

how can i use limit in include model using sequelize


i want to get user's images at limit 2 from Follow model.

Models

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
    }
}

and, join

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'})

so, i tried get user's images from follow by use 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
                        }
                    ]
                }
            ]
        })

but i want to get images at limit 2.

so i tried that

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
                        }
                    ]
                }
            ]
        })

but it makes bugs i cant understand.

images field is a array contain empty object at 4.

all same..

what is the problem?

how can i solve this problem??


Solution

  • You can try :

    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 causes the issues some time on nested level , so it always safe to run that query separately.