3

I have an address and I want to see if it's in the db already, if not, create a new one. I know I can use findOrCreate() here, but let's make it easy and just check why I can't even find the existing address.

  var address = {
    name: req.body.name,
    address1: req.body.address1,
    address2: req.body.address2,
    zip: req.body.zip,
    city: req.body.city,
    country: req.body.country,
    user_id: req.body.user_id
  };

  Address.find({where: address}).then(function(result){
    console.log(result);
  }).catch(function(err){
    console.error(err);
  });

The generated query asks for ... AND user_id = NULLwhich is wrong. It should ask ... AND user_id IS NULL. How can I let sequelize do it right for me? Thanks.

1 Answer 1

5

You should just use the JavaScript null primitive.

var address = {
    name: req.body.name,
    address1: req.body.address1,
    address2: req.body.address2,
    zip: req.body.zip,
    city: req.body.city,
    country: req.body.country,
    user_id: req.body.user_id || null
};

This will generate the query

... user_id IS NULL

Additional information

model.findAll( { where: { some_column : undefined } } );

will generate

... WHERE `some_column` = NULL
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply, I will test this soon.
upvoted. btw you can also use null directly: model.findAll( { where: { some_column : null } } );
Must use "null"

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.