Migration of databases

Hello,
I have a postgres DB having One table consisting of JSONB data.
Parent child relationship is also stored in it.
I want to migrate the whole data to Neo4j.
How can I achieve it?
FYI I will be using Neo4j community edition.

Maybe one of these approaches will help:

To parse the json:

1 Like

@glilienfield
I am writing a function to create nodes and relationships.
My observations are when I am using one script for creating nodes and relationships:

  1. In first time execution it creates nodes and in second execution it creates nodes again and also relationships
    My observations are when I am using two separate script for creating nodes and relationships:

  2. I have executed node creation script: Worked fine

  3. I have executed relation creation script: Worked fine
    FYI I am using tx.run

Can you post your scripts?

In general, use “merge” instead of “create” if the entity could already exist and you don’t want to create it again. Merge does a match first, then a create if a match wasn’t found.

Do the same with your relationship creation. Do something like the following:

Match(n)
Match(m)
Merge(n)-[]->(m)
1 Like

these are the two functions that works fine separately.
Function 1:
def create_node(tx, item, p_id):
label = item.get("Type")
UN_id = item.get("UN")
QN_id = item.get("QN")

tx.run(
    f"CREATE (n:{label} {{P_ID: $p_id, Name: $Name, UN: $UN, QN: $QN}})",
    p_id=p_id,
    Name=item.get("Name"),
    UN=item.get("UN"),
    QN=item.get("QN")
    )

Function 2:
def create_rel(tx, item, p_id):
label = item.get("Type")
UN_id = item.get("UN")
QN_id = item.get("QN")

if QN_id != "None":
    try:
       QN_id = int(QN_id)
            # Match the current node as the QN and the QN node with the specified ID
            tx.run(
                f"MATCH (QN),(QN) where QN.P_ID = $p_id AND QN.P_ID = $QN_id "
                "CREATE (QN)-[:HAS_REL]->(QN)", p_id=p_id, QN_id=QN_id
            )
            print(f"Relationship created: {{P_ID: {p_id}}} -[:HAS_REL]-> {{P_ID: {int(QN_id)}}}")
        except Exception as e:
            print(f"Error creating relationship: {e}")

But when they are merged together, they are not giving expected output.
Please ignore the typo mistakes. I have written them so that you can get a general idea.

@glilienfield Please look into this.

Not sure what “merged” together means? What is the expected result and what do you get instead?

BTW, it looks like the two nodes you are matching for and using to create a relationship between are bound to the same variable QN. Is this valid?

Merged together means when I put these two snippets inside one function.

Result I get when I execute merged function: when I execute the program first time nodes are created, when I execute the program second time nodes are also created again, then there are also relationships that are created but the mapping is wrong.

Expected result is: Nodes should be created and relationships should be mapped correctly in first execution of the program.

Sorry for the typing mistake. Let me rewrite it.
if QN_id != "None":
try:
UN_id = int(UN_id)
# Match the current node as the QN and the UN node with the specified ID
tx.run(
f"MATCH (QN),(UN) where QN.P_ID = $p_id AND UN.P_ID = $UN_id "
"CREATE (QN)-[:HAS_REL]->(UN)", p_id=p_id, UN_id=UN_id
)
print(f"Relationship created: {{P_ID: {p_id}}} -[:HAS_REL]-> {{P_ID: {int(UN_id)}}}")
except Exception as e:
print(f"Error creating relationship: {e}")

@glilienfield I tried using merge statement, but the result is same as explained earlier.