Dynamic relationships

Any idea why:

MATCH (A)-[:property]->(P)-[:type]->(T) 
MATCH (P)-[:value]->(V)
WITH A AS AU, V AS VU, T.code AS relName 
    CREATE (AU)-[:$(relName) {p:'1'}]->(VU)

... all the relationships created are the first node (T) found?

you got any sample data and the result that you can share?

No data, but i think it is a bug ... If I do a return, I get a triples

MATCH (A)-[:property]->(P)-[:type]->(T) 
MATCH (P)-[:value]->(V)
WITH A.uuid AS AU, V.uuid AS VU, T.code AS relName 
RETURN AU, VU,relName

I ended up using the apoc.text.join to generate all the queries one by one:

MATCH (A) WHERE A.uuid='54:6b65977c-9dd8-4989-80f4-225b96258645:13244' MATCH (V) WHERE V.uuid ='1f51a86f-9389-498e-80b8-788da98a6857' WITH A, V CREATE (A)-[:size {p:1}]->(V);

T.code = "size" is the first node's property I need to use and the one ALL my relationships get in the origial query.

Do you have multiple paths between a given node A and node V? I assume each P has only one relationship to a node T.

Without seeing the data, I can't make an assessment of what is happening. I may look at refactoring the query to something like this, as you are really looking for the relationship between an A node and a V node, and you want to use the related T node to get the relationship type between a new path connecting A and V directly.

MATCH (A)-[:property]->(P)-[:value]->(V)
WITH A.uuid AS AU, V.uuid AS VU, [(P)-[:type]->(T)|T.code][0] AS relName 
RETURN AU, VU,relName

It looks like this:

And what i wanted:

I created your scenario:

create(x0:Node{uuid:0,name:"A"}),(x1:Node{uuid:1, name:"P1"}),(x2:Node{uuid:2,name:"V1"}),(x3:Node{uuid:3,name:"V2"}),(x4:Node{uuid:4,name:"P2"}),(x5:Node{uuid:5,name:"T",type:"size"})
create(x0)-[:property]->(x1)-[:value]->(x2),(x1)-[:value]->(x3)
create(x0)-[:property]->(x4),(x1)-[:type]->(x5)

Result of my query:

MATCH (A)-[:property]->(P)-[:value]->(V)
WITH A, V, [(P)-[:type]->(T)|T.type][0] AS relName 
create(A)-[:$(relName)]->(V)

Result of your query:

MATCH (A)-[:property]->(P)-[:type]->(T) 
MATCH (P)-[:value]->(V)
create(A)-[:$(T.type)]->(V)

The results are the same and what I expected. I don't see an issue with your query with this isolated data set. Could there be some other property causing your issue with the "real" data?

I copy/pasted the query from when I did the find the issue (first query), nothing changed ... the label on the first relationship went to ALL nodes A-[rel]->V, not just the first node, and not only to those that had the property.

Yet if i returned the triples, the label was different on each row.

Hi @joshcornejo

Do you know which version of Neo4j you are using? It's definitely possible you are running into a bug with Dynamic Types that was fixed in 5.26.4 LTS and 2025.03.

1 Like
2025-03-13 07:40:24.583+0000 INFO  ======== Neo4j 2025.02.0 ========

Will update!

1 Like