Crafting a sequelize WHERE IN using multiple columns

197
April 25, 2018, at 09:54 AM

In MySQL (And other dbs), you can do a where in query with multiple columns like below:

SELECT * FROM trains WHERE (name, location) IN (('train1', 'us'), ... ,('train2', 'us'));

I'm trying to do this with sequelize:

const selectTrains = [
    {name: 'train1', location: 'us'},
    {name: 'train1', location: 'eu'},
    {name: 'train2', location: 'us'},
];
let dbTrains = await Trains.findAll({ where: { [Op.in]: selectTrains} });

But I get the following error:

UnhandledPromiseRejectionWarning: Error: Invalid value {name: 'train1', location: 'us'}

The documentation didn't really cover this use case. Is this possible to do with the Op.in operator?

Answer 1

You can achieve the same thing with $or :

const selectTrains = [
    {name: 'train1', location: 'us'},
    {name: 'train1', location: 'eu'},
    {name: 'train2', location: 'us'},
];
Trains.findAll({ where: { $or : selectTrains} });
Rent Charter Buses Company
READ ALSO
How to update specific data inside a column based on a pattern with SQL?

How to update specific data inside a column based on a pattern with SQL?

For some reasons, in my database, in a table column, I have some data with an absolute url like this :

151
what does those mysql documentations lines means? “Numeric type overview>BIGINT”

what does those mysql documentations lines means? “Numeric type overview>BIGINT”

"Numeric type overview>BIGINT" Hello pals, I had trying understanding for a long time but can't, please help me with examples of the different cases when mysql can handle BIGINTExamples is always a good way to understand

121
REPEAT Query MySQL

REPEAT Query MySQL

I am having some problems with the REPEAT Query in MySQLI asked a similar question not too long ago but it was never answered

232
How can I interpret html in a view?

How can I interpret html in a view?

Coming from a field of a database I get a result that looks like this

166