{"id":2836,"date":"2015-12-30T01:47:11","date_gmt":"2015-12-30T01:47:11","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2836"},"modified":"2020-04-01T00:43:58","modified_gmt":"2020-04-01T00:43:58","slug":"sequelize-is-null-is-not-null","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/sequelize-is-null-is-not-null\/","title":{"rendered":"Sequelize: IS NULL \/ IS NOT NULL"},"content":{"rendered":"\n<p>Sequelize has a neat syntax for turning SQL WHERE clauses into JSON objects.  The following shows how to create  IS NULL and IS NOT NULL queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IS NOT NULL<\/h2>\n\n\n\n<p>For &#8220;IS NOT NULL&#8221;, try this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> db.Alert.find({\n    where: {\n      UserId: req.params.user_id,\n      url: {\n        $ne: null\n      }\n    }})\n    .then(\n      (alert) => {\n        \/\/ handle result\n      }\n    );\n<\/pre>\n\n\n\n<p>This renders the following SQL (formatted for readability):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT \n   \"id\", \"title\", \"text\", \"domain\", \n   \"lastRun\", \"lastSeen\", \"url\", \n    \"createdAt\", \"updatedAt\", \"UserId\" \nFROM \"Alerts\" AS \"Alert\" \nWHERE \"Alert\".\"UserId\" = '1' \nAND \"Alert\".\"url\" IS NOT NULL \nLIMIT 1;\n<\/pre>\n\n\n\n<p><strong>Note<\/strong>: Quoting the column names forces everything to be case sensitive in Postgres. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>This will force you to quote columns and tables when you query your own database.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">IS NULL<\/h2>\n\n\n\n<p>You can also query for &#8220;is null&#8221;, like so:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">db.Alert.find({\n  where: {\n    UserId: req.params.user_id,\n    url: null\n  }})\n  .then(\n    (alerts) => {\n      res.render('embed', { alerts: alerts });\n    }\n  );\n<\/pre>\n\n\n\n<p>And there you have it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to create  IS NULL and IS NOT NULL queries with Sequelize.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[387,388,389,412,437,504],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2836"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2836"}],"version-history":[{"count":3,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions"}],"predecessor-version":[{"id":6546,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2836\/revisions\/6546"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}