function create_references_flocks_relationship_v2
Creates a REFERENCES_FLOCKS relationship in a Neo4j graph database between a dbo_Treatments node (source) and a dbo_Flocks node (target), with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2162 - 2182
moderate
Purpose
This function establishes a directed relationship in a Neo4j database to link treatment records to flock records. It's designed for database schema management where treatments need to reference specific flocks. The function supports adding custom properties to the relationship edge, making it flexible for storing metadata about the treatment-flock association. It uses Cypher query language to match existing nodes by ID and create the relationship between them.
Source Code
def create_references_flocks_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_FLOCKS relationship from dbo_Treatments to dbo_Flocks"""
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_Flocks {id: $target_id})
CREATE (source)-[r:REFERENCES_FLOCKS]->(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 should be a value that matches the 'id' property of an existing dbo_Treatments node in the Neo4j database. Type is typically string or integer depending on the database schema.
target_id: The unique identifier of the target node (dbo_Flocks). This should be a value that matches the 'id' property of an existing dbo_Flocks node in the Neo4j database. Type is typically string or integer depending on the database schema.
properties: Optional dictionary of key-value pairs to set as properties on the created relationship. Keys should be valid property names (strings), and values can be any Neo4j-compatible data type (strings, numbers, booleans, etc.). If None or not provided, no additional properties are set on the relationship. Default is None.
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 relationship object is typically a Neo4j Record containing relationship metadata.
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_flocks_relationship(
source_id=123,
target_id=456
)
# Example 2: Create relationship with properties
result = create_references_flocks_relationship(
source_id=123,
target_id=456,
properties={
'created_date': '2024-01-15',
'treatment_type': 'vaccination',
'dosage': 10.5
}
)
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 silent failures
- Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, special characters)
- Consider adding error handling around the run_query call to catch connection issues or query failures
- Be aware of potential Cypher injection if source_id or target_id come from untrusted sources; the parameterized query approach used here helps mitigate this
- Check if a relationship already exists before creating a new one to avoid duplicate relationships unless duplicates are intentional
- The function uses string formatting for the SET clause which could be vulnerable if property keys are user-controlled; ensure property keys are validated
- Consider wrapping this in a transaction if creating multiple relationships to ensure atomicity
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_flocktypes_relationship_v2 90.0% similar
-
function create_references_flocks_relationship_v1 88.7% similar
-
function create_references_establishment_relationship_v5 87.4% similar
-
function create_references_flocktypes_relationship_v1 87.0% similar
-
function create_references_flocks_relationship 86.3% similar