简体   繁体   中英

Sequelize where condition for nested models

I have a database with notes and users.Now i want to get all notes where note content = something or users.name = something.I can write this query easily with SQL but couldn't get it working with sequlizer.

Note.findAll({
   include: [
             { model: Users, where: { name: 'something' }}
    ]
})

expected query is

select * from users,notes where notes.content='something' or users.name='something'

The issue is Note is not inside include so I cannot use

where: {
     '$or':{

My question is how to have where with or condition on nested table using sequelize

Note.findAll({
    where: {
        [Op.or]: [
            { content: { [Op.like]: 'something' } },
            { '$users.name$': { [Op.like]: 'john' } },
        ]
    },
    include: [
        { model: Users, as: 'users', required: true }
    ]
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM