function create_references_tests_relationship
Creates a REFERENCES_TESTS relationship in a Neo4j graph database between a LIMS_SampleTypeTests node and a LIMS_Tests node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1394 - 1414
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database from a source node (LIMS_SampleTypeTests) to a target node (LIMS_Tests). It's designed for Laboratory Information Management Systems (LIMS) to link sample type tests to their corresponding test definitions. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.
Source Code
def create_references_tests_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_TESTS relationship from LIMS_SampleTypeTests to LIMS_Tests"""
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_SampleTypeTests {id: $source_id})
MATCH (target:LIMS_Tests {id: $target_id})
CREATE (source)-[r:REFERENCES_TESTS]->(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_SampleTypeTests node. This should be a value that matches the 'id' property of an existing LIMS_SampleTypeTests node in the Neo4j database.
target_id: The unique identifier of the target LIMS_Tests node. This should be a value that matches the 'id' property of an existing LIMS_Tests node in the Neo4j database.
properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_TESTS 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 source or target 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 available and Neo4j is configured
# Create a simple relationship without properties
result = create_references_tests_relationship(
source_id="sample_type_test_001",
target_id="test_definition_042"
)
# Create a relationship with properties
relationship_props = {
"created_date": "2024-01-15",
"created_by": "admin",
"is_mandatory": True,
"order": 1
}
result = create_references_tests_relationship(
source_id="sample_type_test_002",
target_id="test_definition_043",
properties=relationship_props
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship - check if nodes exist")
Best Practices
- Ensure both source and target nodes exist in the database before calling this function to avoid returning None
- Validate source_id and target_id before passing them to prevent query failures
- Use meaningful property names in the properties dictionary that follow Neo4j naming conventions
- Handle the None return value appropriately in calling code to detect failed relationship creation
- Consider wrapping this function call in try-except blocks to handle potential Neo4j connection errors
- The function uses parameterized queries which is good for security, but ensure the run_query function also properly handles parameters
- Be aware that this function will fail silently (return None) if nodes don't exist - consider adding validation or error logging
- Avoid creating duplicate relationships by checking if the relationship already exists before calling this function
- Property values should be Neo4j-compatible types (avoid complex Python objects that can't be serialized)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_tests_relationship_v1 93.4% similar
-
function create_references_sampletypes_relationship 92.7% similar
-
function create_references_samples_relationship 90.9% similar
-
function create_references_sampletypes_relationship_v1 89.6% similar
-
function create_references_samples_relationship_v1 89.3% similar