function create_references_establishment_relationship_v3
Creates a REFERENCES_ESTABLISHMENT relationship in a Neo4j graph database between a dbo_TNV node and a dbo_Establishment node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2098 - 2118
moderate
Purpose
This function establishes a directed relationship in Neo4j from a source node (dbo_TNV) to a target node (dbo_Establishment). It's designed for linking TNV (likely a business or entity reference) records to their corresponding establishment records in a graph database. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.
Source Code
def create_references_establishment_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_ESTABLISHMENT relationship from dbo_TNV to dbo_Establishment"""
props = ""
if properties:
props_list = ', '.join([f"r.{prop} = ${prop}" for prop in properties.keys()])
props = f"SET {props_list}"
query = f"""
MATCH (source:dbo_TNV {id: $source_id})
MATCH (target:dbo_Establishment {id: $target_id})
CREATE (source)-[r:REFERENCES_ESTABLISHMENT]->(target)
{props}
RETURN r
"""
params = {"source_id": source_id, "target_id": target_id}
if properties:
params.update(properties)
result = run_query(query, params)
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
source_id |
- | - | positional_or_keyword |
target_id |
- | - | positional_or_keyword |
properties |
- | None | positional_or_keyword |
Parameter Details
source_id: The unique identifier of the source dbo_TNV node. This should match the 'id' property of an existing dbo_TNV node in the Neo4j database. Expected type: string or integer depending on your database schema.
target_id: The unique identifier of the target dbo_Establishment node. This should match the 'id' property of an existing dbo_Establishment node in the Neo4j database. Expected type: string or integer depending on your database schema.
properties: Optional dictionary containing key-value pairs to set as properties on the created relationship. Keys should be valid property names, and values can be any Neo4j-compatible data type (strings, numbers, booleans, etc.). Default is None, meaning no additional properties will be set.
Return Value
Returns the created relationship object (r) from Neo4j if successful, containing the relationship details and any properties set. Returns None if the query fails or no relationship is created (e.g., if source or target nodes don't exist). The return type is typically a Neo4j Record object or None.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j is configured
# Example 1: Create relationship without properties
result = create_references_establishment_relationship(
source_id="TNV123",
target_id="EST456"
)
# Example 2: Create relationship with properties
properties = {
"reference_date": "2024-01-15",
"confidence_score": 0.95,
"verified": True
}
result = create_references_establishment_relationship(
source_id="TNV123",
target_id="EST456",
properties=properties
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship")
Best Practices
- Always verify that both source_id and target_id exist in the database before calling this function to avoid silent failures
- Use meaningful property names in the properties dictionary that follow your database schema conventions
- Handle the None return value appropriately to detect when relationship creation fails
- Consider wrapping this function in a try-except block to handle Neo4j connection errors
- Be aware that the function uses string formatting for the Cypher query, but parameters are safely passed through the params dictionary
- Ensure the run_query function is properly implemented with connection pooling and error handling
- Consider adding validation for source_id and target_id before executing the query
- The function does not check for existing relationships - calling it multiple times will create duplicate relationships
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_establishment_relationship_v4 90.8% similar
-
function create_references_establishment_relationship_v2 88.7% similar
-
function create_references_establishment_relationship_v6 87.3% similar
-
function create_references_establishment_relationship_v5 85.0% similar
-
function get_dbo_tnv_with_references_establishment_dbo_establishment 84.9% similar