function create_references_usergroup_relationship
Creates a REFERENCES_USERGROUP relationship in a Neo4j graph database between a dbo_Establishment node (source) and a dbo_UserGroup node (target), with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1810 - 1830
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database to link an Establishment entity to a UserGroup entity. It's designed for database schema operations where establishments need to reference user groups, such as tracking which user groups are associated with specific establishments. The function supports adding custom properties to the relationship edge for storing metadata about the reference.
Source Code
def create_references_usergroup_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_USERGROUP relationship from dbo_Establishment to dbo_UserGroup"""
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_Establishment {id: $source_id})
MATCH (target:dbo_UserGroup {id: $target_id})
CREATE (source)-[r:REFERENCES_USERGROUP]->(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_Establishment node. This should be a value that matches the 'id' property of an existing Establishment node in the Neo4j database.
target_id: The unique identifier of the target dbo_UserGroup node. This should be a value that matches the 'id' property of an existing UserGroup node in the Neo4j database.
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.). 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 execution fails or returns no results. The relationship object typically includes metadata like relationship type, properties, and connected node references.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is available and Neo4j is configured
# Create a simple relationship without properties
relationship = create_references_usergroup_relationship(
source_id="EST123",
target_id="UG456"
)
# Create a relationship with properties
relationship_with_props = create_references_usergroup_relationship(
source_id="EST123",
target_id="UG456",
properties={
"created_date": "2024-01-15",
"reference_type": "primary",
"active": True
}
)
if relationship_with_props:
print("Relationship created successfully")
else:
print("Failed to create relationship")
Best Practices
- Ensure both source and target nodes exist in the database before calling this function to avoid query failures
- Validate source_id and target_id parameters before passing them to prevent injection attacks or invalid queries
- 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 call in try-except blocks to handle Neo4j connection errors or query execution failures
- Be aware that the function uses string interpolation for the Cypher query which could be vulnerable if the props variable is not properly sanitized (though parameterized queries are used for IDs and properties)
- Ensure the run_query function properly manages database connections and transactions
- Consider adding duplicate relationship checks if your use case requires unique relationships between nodes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_establishment_relationship_v2 85.2% similar
-
function create_references_establishment_relationship_v4 85.1% similar
-
function get_dbo_establishment_with_references_usergroup_dbo_usergroup 83.6% similar
-
function create_references_establishment_relationship_v3 82.4% similar
-
function create_references_establishment_relationship_v6 81.5% similar