Skip to content

Commit

Permalink
Allow array indices / lookup nodes to be used as dict keys.
Browse files Browse the repository at this point in the history
Fixes #2158
  • Loading branch information
coleifer committed Apr 24, 2020
1 parent 6a58f85 commit 7eb7498
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -6237,8 +6237,10 @@ def _normalize_data(cls, data, kwargs):
field = (key if isinstance(key, Field)
else cls._meta.combined[key])
except KeyError:
raise ValueError('Unrecognized field name: "%s" in %s.' %
(key, data))
if not isinstance(key, Node):
raise ValueError('Unrecognized field name: "%s" in %s.'
% (key, data))
field = key
normalized[field] = data[key]
if kwargs:
for key in kwargs:
Expand Down
3 changes: 3 additions & 0 deletions playhouse/postgres_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __init__(self, node, parts):
def clone(self):
return type(self)(self.node, list(self.parts))

def __hash__(self):
return hash((self.__class__.__name__, id(self)))


class _JsonLookupBase(_LookupNode):
def __init__(self, node, parts, as_json=False):
Expand Down
12 changes: 12 additions & 0 deletions tests/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ def test_index_expression(self):
q = ArrayModel.select().where(ArrayModel.tags[last_tag] > 'd')
self.assertEqual([a.id for a in q], [am_ids[1]])

def test_hashable_objectslice(self):
ArrayModel.create(tags=[], ints=[[0, 1], [2, 3]])
ArrayModel.create(tags=[], ints=[[4, 5], [6, 7]])
n = (ArrayModel
.update({ArrayModel.ints[0][0]: ArrayModel.ints[0][0] + 1})
.execute())
self.assertEqual(n, 2)

am1, am2 = ArrayModel.select().order_by(ArrayModel.id)
self.assertEqual(am1.ints, [[1, 1], [2, 3]])
self.assertEqual(am2.ints, [[5, 5], [6, 7]])

def test_array_get_set(self):
am = self.create_sample()
am_db = ArrayModel.get(ArrayModel.id == am.id)
Expand Down

0 comments on commit 7eb7498

Please sign in to comment.