Search code examples
memgraphdb

Issue with Importing .cypherl file in Memgraph Lab


I'm a novice with Memgraph and was attempting to initialize a database by importing a .cypherl file in Memgraph Lab. Individual nodes are imported successfully, the relationships seem to reference nodes without any labels. When I directly execute the same queries in the Memgraph Lab's query editor, everything works perfectly. What could be the reason for this discrepancy?

Here's my code:

CREATE (a:Article {title: 'On Writing', author: 'Hemingway'})
CREATE (m:Magazine {title: 'Edition 1'})
CREATE (m)-[:MAGAZINE_OF]->(a)
CREATE (v0:Volume {title: 'V1', issues: 1})
CREATE (v0)-[:VOLUME_OF]->(m)
CREATE (v1:Volume {title: 'V2', issues: 2});

If I use Memgraph Lab's Import & Export UI to import this file I would expect to get a database with appropriately linked nodes and a schema identical to what's achieved when the queries are run directly in the query editor.


Solution

  • The .cypherl files are processed in Memgraph Lab so that each line is executed as a separate transaction. When the line CREATE (m)-[:MAGAZINE_OF]->(a) is executed, it ends up creating a relationship between two nodes with no labels.

    To get around this issue you can structure your .cypherl file to first match any previously defined nodes or relationships before creating new ones:

    CREATE (a:Article {title: 'On Writing', author: 'Hemingway'});
    CREATE (m:Magazine {title: 'Edition 1'});
    MATCH (m:Magazine {title: 'Edition 1'}), (a:Article {title: 'On Writing', author: 'Hemingway'}) CREATE (m)-[:MAGAZINE_OF]->(a);
    MATCH (m:Magazine {title: 'Edition 1'}) CREATE (v0:Volume {title: 'V1', issues: 1})-[:VOLUME_OF]->(m)
    CREATE (v1:Volume {title: 'V2', issues: 2});
    

    When you import this file, you should see the expected dataset in Memgraph Lab.

    If you export a .cypherl file from your existing dataset in Memgraph, navigate to the Import & Export tab. The exported .cypherl file might look somewhat different but will be structured in a way to ensure correct importing.

    Before importing, the .cypherl file initially creates indexes and removes them in the end. Importing will first define all nodes and then execute the necessary matching based on generated ids.