Issue: Testing IS NULL for JSONFields fails
Author: alexlatchford
State: closed
Created: 2015-09-27 03:03:02

Hi Charles,

Just found a nice little one.

```
from peewee import *
from playhouse.postgres_ext import JSONField

db = PostgresqlDatabase('mydb', user="postgres")

class MyModel(Model):
    data = JSONField(null=True)

    class Meta:
        database = db

MyModel.create_table()

query = MyModel.select().where(MyModel.data >> None)
for d in query:
    print d.data
```

Will fail with the following:

```
alexlatchford@Alexs-Air Desktop $ python peewee_test.py 
Traceback (most recent call last):
  File "peewee_test.py", line 15, in <module>
    for d in query:
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 2846, in __iter__
    return iter(self.execute())
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 2839, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 2535, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 3339, in execute_sql
    self.commit()
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 3185, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/Users/alexlatchford/Code/peewee/peewee.py", line 3331, in execute_sql
    cursor.execute(sql, params or ())
peewee.ProgrammingError: syntax error at or near "'null'"
LINE 1: ...1"."data" FROM "mymodel" AS t1 WHERE ("t1"."data" IS 'null')
```

Did a little bit of digging to see if it was an easy fix but couldn't seem to find the cause of the issue, sorry!

Cheers,
Alex

PS. Tested with the latest copy from master


Hello, I'm not sure if you've wrapped this up into your package yet, but in version 2.5.1 this is still an issue with [](as opposed to None):

```
Traceback (most recent call last):
  File "/opt/corndog/pagerDutyDumper.py", line 73, in run
    self.dump()
  File "/opt/corndog/pagerDutyDumper.py", line 57, in dump
    dbalerts = set([int(i.alertId) for i in dbalerts])
  File "/usr/lib/python2.7/site-packages/peewee.py", line 2689, in __iter__
    @returns_clone
  File "/usr/lib/python2.7/site-packages/peewee.py", line 2682, in execute
    if args:
  File "/usr/lib/python2.7/site-packages/peewee.py", line 2371, in _execute
    instance._prepare_instance()
  File "/usr/lib/python2.7/site-packages/playhouse/postgres_ext.py", line 369, in execute_sql
    self.rollback()
  File "/usr/lib/python2.7/site-packages/peewee.py", line 2922, in __exit__
    if not db.returning_clause:
  File "/usr/lib/python2.7/site-packages/playhouse/postgres_ext.py", line 360, in execute_sql
    cursor = self.get_cursor(name=str(uuid.uuid1()))
ProgrammingError: syntax error at or near ")"
LINE 1: ...t1"."hosted" FROM "alert" AS t1 WHERE ("t1"."alertId" IN ())
                                                                     ^
```

My offending lines are 

```
# nums gets generated above
print 'Nums is "{}"'.format(nums)
dbalerts = psql.Alert.select().where(psql.Alert.alertId << nums)
dbalerts = set([int(i.alertId) for i in dbalerts])
```

Nums is "[]"

For now I've wrapped up the query and list comprehension in a try/except.

Thanks!

EDIT: Huh. I `pip install --upgrade peewee`'d this morning and just for giggles, did it again. It's at 2.6.4 now.


"<<" is "in", whereas ">>" is "IS".


Fair enough, however this still seems (to me, anyway) to be a problem. I can submit a separate issue if you'd like.


Oh, I see, you weren't trying to check for `NULL` but passing an empty list. I'd just check the list before running the query, that seems the best path forward.