Note that there are some explanatory texts on larger screens.

plurals
  1. POPeewee syntax for selecting on null field
    primarykey
    data
    text
    <p>I have researched this everywhere and can't seem to find an answer. I hope I haven't duplicated this (as it's my first question on SO).</p> <p>I am trying to write a select query with Peewee that would normally go ... WHERE foo = NULL; in SQL world.</p> <p>MySQL looks like this:</p> <pre><code>+-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | user | varchar(30) | NO | | NULL | | | peer | varchar(30) | NO | | NULL | | | deleted | date | YES | | NULL | | | confirmed | date | YES | | NULL | | +-----------+-------------+------+-----+---------+----------------+ </code></pre> <p>My select query looks like this:</p> <pre><code>Peers.select().where(Peers.user == 'foo' and Peers.deleted is None) </code></pre> <p>But it doesn't work! I've tried <code>Peers.deleted == ""</code> and <code>Peers.deleted == "NULL"</code>. The MySQL syntax should end in <code>WHERE deleted is NULL;</code> but nothing in Peewee seems to be doing that.</p> <p>Can anyone help? What am I missing from the docs?</p> <p>Updated from Foo Bar User's comment: <code>and not Peers.deleted</code> didn't work, but it led me to more information. It seems that peewee wants the <code>where</code> clauses chained together. So instead of </p> <p><code>Peers.select().where(Peers.user == 'foo' and Peers.deleted is None)</code></p> <p>it should be:</p> <p><code>Peers.select().where(Peers.user == 'foo').where(Peers.deleted is None)</code></p> <p>Sadly, that still doesn't yield the right syntax to select on null rows in deleted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload