function create_references_interventionprotocols_relationship
Creates a directed REFERENCES_INTERVENTIONPROTOCOLS relationship in a Neo4j graph database from a dbo_InterventionProtocolFlocks node to a dbo_InterventionProtocols node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2066 - 2086
simple
Purpose
This function establishes a relationship between intervention protocol flocks and intervention protocols in a Neo4j graph database. It's designed for data modeling scenarios where flocks reference specific intervention protocols, allowing for optional metadata to be stored on the relationship itself. The function uses Cypher queries to match existing nodes by ID and create the relationship between them.
Source Code
def create_references_interventionprotocols_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_INTERVENTIONPROTOCOLS relationship from dbo_InterventionProtocolFlocks to dbo_InterventionProtocols"""
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_InterventionProtocolFlocks {id: $source_id})
MATCH (target:dbo_InterventionProtocols {id: $target_id})
CREATE (source)-[r:REFERENCES_INTERVENTIONPROTOCOLS]->(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_InterventionProtocolFlocks). This value is used to match the source node in the Neo4j database. Expected to be a string or integer that corresponds to the 'id' property of the source node.
target_id: The unique identifier of the target node (dbo_InterventionProtocols). This value is used to match the target node in the Neo4j database. Expected to be a string or integer that corresponds to the 'id' property of the target 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 on it. 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 function is defined and Neo4j is configured
# Example 1: Create relationship without properties
result = create_references_interventionprotocols_relationship(
source_id=123,
target_id=456
)
# Example 2: Create relationship with properties
properties = {
'created_date': '2024-01-15',
'priority': 'high',
'notes': 'Primary intervention protocol'
}
result = create_references_interventionprotocols_relationship(
source_id=123,
target_id=456,
properties=properties
)
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 creating orphaned relationships
- 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 when relationship creation fails
- 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, but uses parameterized queries for values to prevent injection attacks
- The function depends on an external run_query function which must be properly configured with Neo4j credentials
- Consider adding validation to check if the relationship already exists before creating a duplicate
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_flocks_relationship_v1 91.8% similar
-
function get_dbo_interventionprotocolflocks_with_references_interventionprotocols_dbo_interventionprotocols 82.8% similar
-
function create_dbo_interventionprotocolflocks 81.9% similar
-
function get_dbo_interventionprotocolflocks_with_references_flocks_dbo_flocks 79.4% similar
-
function create_references_flocks_relationship_v2 77.1% similar