The statement generated by utils.dictToWhere is not suitable for None value
g1itch opened this issue · comments
Dmitri Bogomolov commented
The statement should be IS NULL
for None value, not = NULL
. As a result BDObject.findBy()
returns []
in that case. The following test succeeds if name is a string:
diff --git a/twistar/tests/test_dbobject.py b/twistar/tests/test_dbobject.py
index 6611e90..d7001d9 100644
--- a/twistar/tests/test_dbobject.py
+++ b/twistar/tests/test_dbobject.py
@@ -84,6 +84,12 @@ class DBObjectTest(unittest.TestCase):
resultids = [result.id for result in results]
self.assertEqual(ids, resultids)
+ @inlineCallbacks
+ def test_null(self):
+ name = None
+ user = yield User(first_name=name).save()
+ users = yield User.findBy(first_name=name)
+ self.assertEqual(user, users[0])
@inlineCallbacks
def test_count(self):
Dmitri Bogomolov commented
I think a solution may be like this
diff --git a/twistar/utils.py b/twistar/utils.py
index 91e7ee1..8b5c1f1 100644
--- a/twistar/utils.py
+++ b/twistar/utils.py
@@ -67,7 +67,8 @@ def dictToWhere(attrs, joiner="AND"):
if len(attrs) == 0:
return None
- wheres = map(lambda name: "(%s = ?)" % name, attrs.keys())
+ wheres = ["(%s = ?)" % key if val else "(%s IS ?)" % key
+ for key, val in attrs.iteritems()]
return [(" %s " % joiner).join(wheres)] + attrs.values()
Brian Muller commented
Great catch! Thanks!