Skip to content

Commit

Permalink
Fixed __primaryvalue__ for composite keys
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed May 28, 2021
1 parent 9676d08 commit 785512e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion docs/ogm/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Each instance may contain attributes that represent labels, nodes or related obj
The primary node label used for Cypher `MATCH` and `MERGE`
operations. By default the name of the class is used.

This value can also be a tuple, in which case a composite primary
label is implied.

.. attribute:: __primarykey__

The primary property key used for Cypher `MATCH` and `MERGE`
Expand All @@ -65,11 +68,15 @@ Each instance may contain attributes that represent labels, nodes or related obj
:meth:`.Graph.create` and :meth:`.Graph.merge` on :class:`.Model`
instances.

This value can also be a tuple, in which case a composite primary
key is implied.

.. attribute:: __primaryvalue__

The value of the property identified by :attr:`.__primarykey__`.
If the key is ``"__id__"`` then this value returns the internal
node ID.
node ID. If the primary key is a tuple, then a corresponding
tuple of property values is returned.

.. attribute:: __node__

Expand Down
2 changes: 2 additions & 0 deletions py2neo/ogm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ def __primaryvalue__(self):
primary_key = self.__primarykey__
if primary_key == "__id__":
return node.identity
elif isinstance(primary_key, tuple):
return tuple(node[key] for key in primary_key)
else:
return node[primary_key]

Expand Down
10 changes: 10 additions & 0 deletions test/integration/ogm/test_ogm.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,13 @@ def test_push(repo, thing):
repo.push(thing)
assert thing.__node__.graph is repo.graph
assert thing.__node__.identity is not None


def test_primary_value_with_single_key(repo):
person = UniquePerson()
assert person.__primaryvalue__ == "Alice"


def test_primary_value_with_composite_key(repo):
person = UniqueFullyNamedPerson()
assert person.__primaryvalue__ == ("Alice", "Smith")

0 comments on commit 785512e

Please sign in to comment.