function create_references_sampletypes_relationship
Creates a directed REFERENCES_SAMPLETYPES relationship in a Neo4j graph database from a LIMS_SampleTypeTests node to a LIMS_SampleTypes node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1362 - 1382
moderate
Purpose
This function establishes a relationship between sample type tests and sample types in a Laboratory Information Management System (LIMS) graph database. It allows linking test configurations to their corresponding sample types, with the ability to add custom properties to the relationship for additional metadata. The function is designed for building or maintaining a graph-based LIMS data model where tests reference specific sample types.
Source Code
def create_references_sampletypes_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_SAMPLETYPES relationship from LIMS_SampleTypeTests to LIMS_SampleTypes"""
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_SampleTypes {id: $target_id})
CREATE (source)-[r:REFERENCES_SAMPLETYPES]->(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. Expected type: string or integer depending on your database schema.
target_id: The unique identifier of the target LIMS_SampleTypes node. This should be a value that matches the 'id' property of an existing LIMS_SampleTypes node in the Neo4j database. Expected type: string or integer depending on your 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 omitted, no additional properties are set on the relationship. Default: 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 to create the relationship (e.g., if source or target nodes don't exist). The relationship object typically includes relationship type, properties, and node references.
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_sampletypes_relationship(
source_id='test_001',
target_id='sample_type_A'
)
# Example 2: Create relationship with properties
result = create_references_sampletypes_relationship(
source_id='test_002',
target_id='sample_type_B',
properties={
'created_date': '2024-01-15',
'is_active': True,
'priority': 1
}
)
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
- Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, special characters)
- 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 or query execution failures
- Be aware that this function uses string interpolation for the Cypher query, which is safe here due to parameterized queries, but the properties SET clause construction should be reviewed for security
- Ensure the run_query function properly manages database connections and transactions
- Consider adding duplicate relationship checks if your use case requires preventing multiple REFERENCES_SAMPLETYPES relationships between the same nodes
- Use meaningful property names that align with your LIMS data model conventions
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_sampletypes_relationship_v1 96.1% similar
-
function create_references_tests_relationship 92.7% similar
-
function create_references_samples_relationship 90.0% similar
-
function create_references_samples_relationship_v1 89.0% similar
-
function create_references_tests_relationship_v1 84.0% similar