function create_references_tests_relationship_v1
Creates a REFERENCES_TESTS relationship in a Neo4j graph database between a LIMS_Testparameters node and a LIMS_Tests node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1618 - 1638
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database from a test parameter node to a test node, allowing the database to track which test parameters reference which tests. It supports adding custom properties to the relationship edge and is part of a LIMS (Laboratory Information Management System) data model.
Source Code
def create_references_tests_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_TESTS relationship from LIMS_Testparameters 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_Testparameters {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_Testparameters node. This should be a value that matches the 'id' property of an existing LIMS_Testparameters 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. 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. The relationship object is the first element from the result array.
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
relationship = create_references_tests_relationship(
source_id="TP001",
target_id="TEST001"
)
# Create a relationship with properties
relationship_with_props = create_references_tests_relationship(
source_id="TP002",
target_id="TEST002",
properties={
"created_date": "2024-01-15",
"created_by": "admin",
"priority": "high"
}
)
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 that source_id and target_id are not None or empty before calling
- Use consistent property naming conventions when passing the properties dictionary
- Handle the None return value to detect when relationship creation fails
- Consider wrapping this function in a try-except block to handle Neo4j connection errors
- Be aware that this function uses string interpolation for the Cypher query, which is safe here due to parameterized queries for values
- Ensure the run_query function properly handles database transactions and connection management
- Consider adding duplicate relationship checks if your use case requires preventing multiple REFERENCES_TESTS relationships between the same nodes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_tests_relationship 93.4% similar
-
function create_references_parameters_relationship_v1 92.9% similar
-
function create_references_testparameters_relationship 92.3% similar
-
function create_references_parameters_relationship 87.3% similar
-
function create_references_samples_relationship 85.3% similar