1

I am using sequelize v6 on my nodejs/express application. I am struggling to use an attribute of my model in the where clause of my findAll query.

What I want to achieve in SQL:

SELECT field1, field2, field3 FROM myTable
WHERE field2 != field3

what i have in my javascript:

const resultArray = myTable.findAll({
     attributes: [
         "field1",
         "field2",
         "field3",],
     where: {
        field2: { [Op.ne]: {field3}},
     })

What I am after is the right context to reference field3 in the [Op.ne] parameter.

1 Answer 1

1

Just use Sequelize.col to indicate a reference to a column for Sequelize:

const resultArray = myTable.findAll({
     attributes: [
         "field1",
         "field2",
         "field3",],
     where: {
        field2: { [Op.ne]: Sequelize.col('field3') },
     })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.