Search code examples
pythonneo4jcyphercoalescepy2neo

Unable to set list type value in COALESCE pypher


I want to build query like through pypher

set entity.birth_date = coalesce(VALUE + entity.birth_date, entity.birth_date , [] + VALUE)

VALUE is string like '38'

What I've tried is:

from pypher import Pypher, __
p = Pypher()
p.Merge.node('ent', **node_gr)
p.SET(__.ent.__birth_place__ == __.COALESCE(__.ent.__birth_place__+ 
VALUE,__.ent.__birth_place__,[VALUE]))

it throws below error:

python3.6/site-packages/pypher/builder.py in bind_param(self, value, name)
    196                     name = k
    197                     break
--> 198         elif bind and value in self._bound_params.keys():
    199             for k, v in self._bound_params.items():
    200                 if k == value:

TypeError: unhashable type: 'list'

I've also tried converting [VALUE] to string but then updated value is in incorrect as string


Solution

  • Pypher provides the __.List function to construct a list from a value. For example, the following:

    p.SET(__.ent.__birth_place__ == __.COALESCE(__.ent.__birth_place__ + VALUE,__.ent.__birth_place__, __.List() + VALUE))
    

    Yields something similar to:

    MERGE (ent:`MyNode`) SET ent.`birth_place` = coalesce(ent.`birth_place` + $NEO_5288d_0, ent.`birth_place`, [] + $NEO_5288d_0)
    

    Though since List takes an argument that lets you construct a list, a better approach might be:

    p.SET(__.ent.__birth_place__ == __.COALESCE(__.ent.__birth_place__ + VALUE,__.ent.__birth_place__, __.List(VALUE)))
    

    Which yields something like:

    MERGE (ent:`MyNode`) SET ent.`birth_place` = coalesce(ent.`birth_place` + $NEO_e2895_0, ent.`birth_place`, [$NEO_e2895_0])