Help - changing node property type

I made a mistake in creating my nodes: Novel and Session...I had mistakenly created the unique ID values as both string and integer value. I was trying to create the graph from 2 different csv files

I tried running a query to change all string values to integer values but got an error:

Is this error happening because I setup contraints on ItemId and SessionId?

Any advice will be helpful

Try using a type predicate:

match(n:Novel)
where n.ItemId IS NOT :: Integer
set n.ItemId = toInteger(n.ItemId)
return n

In your error, have you added nodes for both the same value as an integer and as a string? For these cases, you will need to remove one of the duplicates. This query finds all Novel nodes that have a String representation of ItemId than finds if another Novel node exists with the same ItemId as an integer. If it does, it deletes the original Novel node leaving the integer one, or sets the ItemId to an integer representation of the ItemId.

match(n:Novel)
where n.ItemId IS :: String
optional match (m:Novel) where m<>n and m.ItemId IS :: Integer and m.ItemId = toInteger(n.ItemId)
with n, m, m is null as doesNotExist
call (n, doesNotExist) {
    with n, doesNotExist
    where doesNotExist
    set n.ItemId = toInteger(n.ItemId)
}
call (n, doesNotExist) {
    with n, doesNotExist
    where not doesNotExist
    detach delete n
}

Test data:

create(:Novel{ItemId: 0}),(:Novel{ItemId: "1"}),(:Novel{ItemId: "2"}),(:Novel{ItemId: "3"}),(:Novel{ItemId: 3})

Before:

After:

Hi Gary
Thank you for your assist once again. For your information, I built the graph from 2 csv file. The first file was imported into Neo4j browser correctly.

I have 48,379 Novel nodes and of those nodes, 12,373 of them have ItemId as string values instead of numeric.

I have attempted to convert the nodes that have string properties to numeric, but got this error:

at this point, I feel like deleting the entire graph and starting again from scratch :sob:
because I feel that the constraint is the reason why I am unable to change the property

Do you have both a Novel node with ItemId = 14 and another one with item ItemId = “14”? That must be the case, as I created a uniqueness constraint on Novel.ItemId and I was able to add a Novel node with ItemId=0 and ItemId="0".

The other code I gave you addressed this case.

Hi Gary,

No, I have some Novel node with ItemId = int.number and some Novel nodes with ItemId='number'.

I've deleted the graph and remade it by combining 2 csv file, dev2.csv and webnovel.csv
(just FYI both csv contains the same data, just different TimeStr )

Now it is as how I wanted it:



1 Like