perform a where in query in bookshelf.js

2019-05-07 10:02发布

问题:

I want to perform a WHERE - IN query/operation but normal where gives error.

I want this

select * from `calendar_event_rsvp` where `event_id` in ('1', '2', '3')

But below code leads to

select * from `calendar_event_rsvp` where `event_id in` = '1', '2', '3'

Code

CalendarEventRSVP.forge()
                .where({
                    "event_id": event_ids
                })

How do i do this in bookshelf.js

回答1:

Try to add the operator:

CalendarEventRSVP.forge()
            .where('event_id', 'in', event_ids)

Or use knex's whereIn:

 CalendarEventRSVP.forge()
            .query({whereIn: {event_id: event_ids}})


回答2:

try query() function on your model.

CalendarEventRSVP.query(function(qb){
    qb.where('event_id' , 'in' , [1,2,3,4]) ;
})
.fetchAll()
.then();