function create_references_parameters_relationship
Creates a REFERENCES_PARAMETERS relationship between two LIMS_Parameters nodes in a Neo4j graph database, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1202 - 1222
moderate
Purpose
This function establishes a directed relationship in a Neo4j database from one LIMS_Parameters node to another, allowing parameters to reference other parameters. It supports adding custom properties to the relationship edge. This is useful for modeling parameter dependencies, hierarchies, or cross-references in a Laboratory Information Management System (LIMS).
Source Code
def create_references_parameters_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_PARAMETERS relationship from LIMS_Parameters to LIMS_Parameters"""
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_Parameters {id: $source_id})
MATCH (target:LIMS_Parameters {id: $target_id})
CREATE (source)-[r:REFERENCES_PARAMETERS]->(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_Parameters node from which the relationship originates. Expected to be a value that matches the 'id' property of a LIMS_Parameters node in the database.
target_id: The unique identifier of the target LIMS_Parameters node to which the relationship points. Expected to be a value that matches the 'id' property of a LIMS_Parameters node in the database.
properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_PARAMETERS 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 set.
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_parameters_relationship(
source_id='PARAM_001',
target_id='PARAM_002'
)
# Example 2: Create relationship with properties
result = create_references_parameters_relationship(
source_id='PARAM_001',
target_id='PARAM_002',
properties={
'reference_type': 'dependency',
'created_date': '2024-01-15',
'weight': 1.5
}
)
if result:
print(f'Relationship created successfully: {result}')
else:
print('Failed to create relationship')
Best Practices
- Ensure both source_id and target_id exist in the database before calling this function to avoid failed queries
- Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, valid characters)
- Be aware of potential Cypher injection vulnerabilities - the function uses parameterized queries for IDs and property values, but property keys are directly interpolated into the query string
- Consider adding error handling around the run_query call to catch database connection issues or constraint violations
- Check if a relationship already exists between the nodes to avoid creating duplicate relationships if that's not desired
- The function assumes run_query is available in scope - ensure this dependency is properly imported or defined
- Property values should be Neo4j-compatible types (strings, numbers, booleans, lists, etc.)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_parameters_relationship_v1 95.2% similar
-
function create_references_testparameters_relationship 90.9% similar
-
function create_references_tests_relationship_v1 87.3% similar
-
function create_references_requests_relationship 82.5% similar
-
function create_references_samples_relationship 80.4% similar