function create_references_samples_relationship_v1
Creates a REFERENCES_SAMPLES relationship in a Neo4j graph database between a LIMS_SampleTestResultDetails node and a LIMS_Samples node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1266 - 1286
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database from a source node (LIMS_SampleTestResultDetails) to a target node (LIMS_Samples). It's designed for Laboratory Information Management Systems (LIMS) to link test result details to their corresponding samples. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.
Source Code
def create_references_samples_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_SAMPLES relationship from LIMS_SampleTestResultDetails to LIMS_Samples"""
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_SampleTestResultDetails {id: $source_id})
MATCH (target:LIMS_Samples {id: $target_id})
CREATE (source)-[r:REFERENCES_SAMPLES]->(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 (LIMS_SampleTestResultDetails). This should be a value that matches the 'id' property of an existing LIMS_SampleTestResultDetails node in the Neo4j database.
target_id: The unique identifier of the target node (LIMS_Samples). This should be a value that matches the 'id' property of an existing LIMS_Samples node in the Neo4j database.
properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_SAMPLES 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 either node doesn't exist). The return type is typically a Neo4j Relationship 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_samples_relationship(
source_id="TEST_RESULT_001",
target_id="SAMPLE_001"
)
# Example 2: Create relationship with properties
result = create_references_samples_relationship(
source_id="TEST_RESULT_002",
target_id="SAMPLE_002",
properties={
"created_date": "2024-01-15",
"created_by": "lab_technician_123",
"confidence_level": 0.95
}
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship")
Best Practices
- Always verify that both source_id and target_id exist in the database before calling this function to avoid silent failures
- 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 in a try-except block to handle Neo4j connection errors
- Validate property values before passing them to ensure they are Neo4j-compatible data types
- Be aware that this function uses string interpolation for the SET clause, but parameters are properly used for values to prevent injection
- Ensure the run_query function is properly implemented with connection pooling and error handling
- 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_samples_relationship 98.5% similar
-
function create_references_sampletypes_relationship_v1 93.8% similar
-
function create_references_requests_relationship 89.9% similar
-
function create_references_tests_relationship 89.3% similar
-
function create_references_sampletypes_relationship 89.0% similar