neo4jrb / neo4j-core

A simple unified API that can access both the server and embedded Neo4j database. Used by the neo4j gem
MIT License
99 stars 80 forks source link

Feature request: Return same ID type for node and relationship #318

Open Joshfindit opened 6 years ago

Joshfindit commented 6 years ago

When querying directly with CYPHER, on a node type with an alternate ID (such as UUID) the node returns uuid by default, but the relationship returns the internal Neo4j ID.

The request is: either have the node include the Neo4j ID, or (the better solution if we are looking to avoid referencing transient Neo4j IDs) have the relationship include the node's config-specified ID

Code example (inline, gist, or repo)

results = Neo4j::ActiveBase.current_session.query("MATCH (n)-[r]-() RETURN n, r LIMIT 3")

Runtime information:

Neo4j database version: 3.1.1 neo4j gem version: 8.0.9 neo4j-core gem version: 7.0.6

cheerfulstoic commented 6 years ago

I would love to be able to use the built-in Neo4j ID for the id_property. It would save a lot of code, but unfortunately the ID can be recycled and thus would cause problems for users of the gem.

We specifically avoided using something like a uuid on relationships because Neo4j (the company via the design of the database) strongly discourages queries which start with relationships. All queries should start with nodes and use relationships to traverse to other nodes.

Does that make sense? I'm open to other suggestions

Joshfindit commented 6 years ago

No, that definitely makes sense; the Neo4j ID is best considered transient. I'd much rather see the results with whatever permanent ID is used on the nodes.

The main problem this request is trying to solve is that the nodes do not return the Neo4j ID, and the relationships don't return any other ID. This means that the output of a raw query cannot be used to reconstruct the relationships.

Joshfindit commented 6 years ago

Essentially;

This is not great but ok:

[{
    "index": 0,
    "n": {
        "identity": {
            "username": "Admin",
            "public_name": null,
            "staged": null,
            "id": "31c7b21c-d0c0-44e4-bb52-73b4c75647bd",
                        "neo_id": "0"
        }
    },
    "r": {
        "id": 0,
        "type": "AUTHORIZES_WEBSESSION",
        "properties": {},
        "start_node_id": 0,
        "end_node_id": 2
    },
    "m": {
        "web_session": {
            "date_created": null,
            "username": "Admin",
            "id": "edc4d3ec-e005-44c2-8072-2aedfeaa0dcc"
                        "neo_id": "2"
        }
    }
}]

This is better:

[{
    "index": 0,
    "n": {
        "identity": {
            "username": "Admin",
            "public_name": null,
            "staged": null,
            "id": "31c7b21c-d0c0-44e4-bb52-73b4c75647bd"
        }
    },
    "r": {
        "id": 0,
        "type": "AUTHORIZES_WEBSESSION",
        "properties": {},
        "start_node_id": "31c7b21c-d0c0-44e4-bb52-73b4c75647bd",
        "end_node_id": "edc4d3ec-e005-44c2-8072-2aedfeaa0dcc"
    },
    "m": {
        "web_session": {
            "date_created": null,
            "username": "Admin",
            "id": "edc4d3ec-e005-44c2-8072-2aedfeaa0dcc"
        }
    }
}]

And this is no good:

[{
    "index": 0,
    "n": {
        "identity": {
            "username": "Admin",
            "public_name": null,
            "staged": null,
            "id": "31c7b21c-d0c0-44e4-bb52-73b4c75647bd"
        }
    },
    "r": {
        "id": 0,
        "type": "AUTHORIZES_WEBSESSION",
        "properties": {},
        "start_node_id": 0,
        "end_node_id": 2
    },
    "m": {
        "web_session": {
            "date_created": null,
            "username": "Admin",
            "id": "edc4d3ec-e005-44c2-8072-2aedfeaa0dcc"
        }
    }
}]