function create_references_houses_relationship_v3
Creates a REFERENCES_HOUSES relationship in a Neo4j graph database between a dbo_Treatments node (source) and a dbo_Houses node (target), with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2194 - 2214
moderate
Purpose
This function establishes a directed relationship in a Neo4j database to link treatment records to house records. It's designed for graph database operations where treatments need to reference specific houses, allowing for optional metadata to be stored on the relationship itself. The function uses Cypher query language to match existing nodes by their IDs and create the relationship between them.
Source Code
def create_references_houses_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_HOUSES relationship from dbo_Treatments to dbo_Houses"""
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_Treatments {id: $source_id})
MATCH (target:dbo_Houses {id: $target_id})
CREATE (source)-[r:REFERENCES_HOUSES]->(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 node (dbo_Treatments). This value is used to match the treatment record in the database. Expected to be a string or integer that corresponds to the 'id' property of a dbo_Treatments node.
target_id: The unique identifier of the target node (dbo_Houses). This value is used to match the house record in the database. Expected to be a string or integer that corresponds to the 'id' property of a dbo_Houses node.
properties: Optional dictionary containing key-value pairs to be 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.). Defaults to None if no properties are needed.
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 result is returned. The relationship object typically includes metadata like relationship type, properties, and node references.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is available and Neo4j is configured
# Example 1: Create relationship without properties
result = create_references_houses_relationship(
source_id=123,
target_id=456
)
# Example 2: Create relationship with properties
result = create_references_houses_relationship(
source_id=123,
target_id=456,
properties={
'reference_date': '2024-01-15',
'reference_type': 'primary',
'confidence_score': 0.95
}
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship")
Best Practices
- Ensure both source_id and target_id exist in the database before calling this function to avoid failed queries
- Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, special characters)
- Handle the None return value appropriately to detect failed relationship creation
- Consider wrapping this function in a try-except block to handle Neo4j connection errors or query failures
- Be aware that this function uses string interpolation for the Cypher query which could be vulnerable if property keys are not sanitized, though parameterized values are used for IDs and property values
- The function assumes run_query is properly configured with Neo4j connection details and handles session management
- Consider checking if the relationship already exists before creating to avoid duplicates if that's a concern for your use case
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_houses_relationship_v1 90.3% similar
-
function create_references_establishment_relationship_v6 85.7% similar
-
function create_references_houses_relationship 84.6% similar
-
function create_references_establishment_relationship_v2 84.0% similar
-
function get_dbo_treatments_with_references_houses_dbo_houses 82.4% similar