I tried to create node and relationship using gqlalchemy in neo4j as per the memgraph documentation but it did not worked
from gqlalchemy import Memgraph, Node, Relationship, Field,Neo4j
from typing import Optional
db = Neo4j(host="localhost", port="7687", username="neo4j", password="12345678")
class User(Node):
id: str = Field(index=True, exist=True, unique=True, db=db)
username: str = Field(index=True, exist=True, unique=True, db=db)
class Language(Node):
name: str = Field(unique=True, db=db)
user = User(id="3", username="John")
db.save_node(user)
language = Language(name="en")
db.save_node(user)
Got an error as
raise Neo4jError.hydrate(**metadata)
neo4j.exceptions.ClientError: {code: Neo.ClientError.Schema.IndexAlreadyExists} {message: There already exists an index (:User {id}). A constraint cannot be created until the index has been dropped.}
On first look, it looks like you are trying to create two identical nodes while having a unique constraint on node id:
user = User(id="3", username="John")
db.save_node(user)
language = Language(name="en")
db.save_node(user)
I would start with that.
On error message:
Neo4j changed how they handle indexes in version 5.x. Not 100% sure about the compatibility of GQLalchemy with version 5.x and above. But from the error message, I would say that GQLalchemy is trying to recreate the index that already exists. This yields an error from the Neo4j side. Take a look at Neo4j 5.x docs: https://neo4j.com/docs/cypher-manual/current/indexes-for-search-performance/
In the meantime, as a workaround, you can try using a cypher to handle indexes with Neo4j and GQLAlchemy. Memgraph is a more compatible with GQLAlchemy since it is made primarily for Memgraph. Consider moving to Memgraph if GQLalchemy is a hard requirement.