function create_references_flocks_relationship
Creates a REFERENCES_FLOCKS relationship in a Neo4j graph database between a LIMS_Samples node and a dbo_Flocks node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1522 - 1542
moderate
Purpose
This function establishes a directed relationship in Neo4j from a source LIMS_Samples node to a target dbo_Flocks node. It's designed for linking laboratory information management system (LIMS) samples to flock records in a database schema. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.
Source Code
def create_references_flocks_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_FLOCKS relationship from LIMS_Samples 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:LIMS_Samples {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 LIMS_Samples node. This should be a value that matches the 'id' property of an existing LIMS_Samples node in the Neo4j database.
target_id: The unique identifier of the target dbo_Flocks node. This should be a value that matches the 'id' property of an existing dbo_Flocks 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 fails or no result is returned (e.g., if source or target nodes don't exist).
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is defined and Neo4j is configured
# Create a simple relationship without properties
result = create_references_flocks_relationship(
source_id='SAMPLE_001',
target_id='FLOCK_123'
)
# Create a relationship with properties
result = create_references_flocks_relationship(
source_id='SAMPLE_002',
target_id='FLOCK_456',
properties={
'reference_date': '2024-01-15',
'confidence_score': 0.95,
'verified': True
}
)
if result:
print(f'Relationship created successfully: {result}')
else:
print('Failed to create relationship')
Best Practices
- Always verify that both source and target nodes exist before calling this function to avoid silent failures
- Use the properties parameter to add metadata about the relationship (e.g., timestamps, confidence scores, verification status)
- Check the return value to ensure the relationship was created successfully before proceeding
- Ensure the run_query function properly handles Neo4j transactions and error handling
- Consider wrapping this function in try-except blocks to handle potential Neo4j connection errors
- The function uses parameterized queries which is good for security, but ensure property keys are validated if they come from user input
- Be aware that the Cypher query syntax has a bug: it should use {id: $source_id} with curly braces, not parentheses
- Consider adding uniqueness constraints or checking for existing relationships to prevent duplicates
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_flocks_relationship_v1 87.1% similar
-
function create_references_flocks_relationship_v2 86.3% similar
-
function create_references_flocktypes_relationship_v2 85.8% similar
-
function get_lims_samples_with_references_flocks_dbo_flocks 84.7% similar
-
function create_references_establishment_relationship_v5 83.4% similar