aWebDeveloper
aWebDeveloper

Reputation: 38352

perform a where in query in bookshelf.js

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

Upvotes: 5

Views: 11690

Answers (2)

Salar
Salar

Reputation: 5509

try query() function on your model.

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

Upvotes: 6

Jcl
Jcl

Reputation: 28272

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

Upvotes: 11

Related Questions