🔍 Code Extractor

function create_references_testparameters_relationship

Maturity: 49

Creates a REFERENCES_TESTPARAMETERS relationship in a Neo4j graph database between a LIMS_SampleTestResults node and a LIMS_Testparameters node, with optional properties on the relationship.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1330 - 1350
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j graph database to link sample test results with their corresponding test parameters. It's designed for Laboratory Information Management Systems (LIMS) to maintain referential integrity between test results and parameter definitions. The function supports adding custom properties to the relationship for additional metadata.

Source Code

def create_references_testparameters_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_TESTPARAMETERS relationship from LIMS_SampleTestResults to LIMS_Testparameters"""
    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_SampleTestResults {id: $source_id})
    MATCH (target:LIMS_Testparameters {id: $target_id})
    CREATE (source)-[r:REFERENCES_TESTPARAMETERS]->(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_SampleTestResults node. This should be a value that matches the 'id' property of an existing LIMS_SampleTestResults node in the Neo4j database.

target_id: The unique identifier of the target LIMS_Testparameters node. This should be a value that matches the 'id' property of an existing LIMS_Testparameters node in the Neo4j database.

properties: Optional dictionary containing key-value pairs to set as properties on the created relationship. Keys should be valid property names, and values can be any Neo4j-compatible data types (strings, numbers, booleans, etc.). Defaults to None if no additional 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 execution fails or returns no results. The relationship object typically includes metadata like relationship type, properties, and connected 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_testparameters_relationship(
    source_id='sample_result_123',
    target_id='test_param_456'
)

# Example 2: Create relationship with properties
result = create_references_testparameters_relationship(
    source_id='sample_result_123',
    target_id='test_param_456',
    properties={
        'created_date': '2024-01-15',
        'created_by': 'lab_technician_01',
        'confidence_level': 0.95
    }
)

if result:
    print(f'Relationship created successfully: {result}')
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 strings before passing to the function
  • Use consistent property naming conventions when passing the properties dictionary
  • Handle the None return value appropriately to detect relationship creation failures
  • Consider wrapping this function call in try-except blocks to handle Neo4j connection errors
  • Verify that the run_query function properly handles database transactions and connection management
  • Be cautious about creating duplicate relationships - consider checking if the relationship already exists before creation
  • Ensure property values in the properties dictionary are compatible with Neo4j data types
  • Use parameterized queries (as implemented) to prevent Cypher injection attacks

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_parameters_relationship_v1 95.2% similar

    Creates a REFERENCES_PARAMETERS relationship in a Neo4j graph database between a LIMS_Testparameters node and a LIMS_Parameters node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_tests_relationship_v1 92.3% similar

    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.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_parameters_relationship 90.9% similar

    Creates a REFERENCES_PARAMETERS relationship between two LIMS_Parameters nodes in a Neo4j graph database, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_samples_relationship 88.2% similar

    Creates a REFERENCES_SAMPLES relationship in a Neo4j graph database between a LIMS_SampleTestResults node and a LIMS_Samples node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_samples_relationship_v1 87.6% similar

    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.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
← Back to Browse