🔍 Code Extractor

function create_references_parameters_relationship_v1

Maturity: 45

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.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1586 - 1606
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j graph database to link test parameters to their corresponding parameter definitions in a Laboratory Information Management System (LIMS). It allows for optional properties to be set on the relationship edge, enabling metadata storage about the reference connection. The function uses Cypher query language to match source and target nodes by ID and create the relationship between them.

Source Code

def create_references_parameters_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_PARAMETERS relationship from LIMS_Testparameters 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_Testparameters {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_Testparameters node. This should be a value that matches the 'id' property of an existing LIMS_Testparameters node in the Neo4j database. Expected type: string or integer depending on the database schema.

target_id: The unique identifier of the target LIMS_Parameters node. This should be a value that matches the 'id' property of an existing LIMS_Parameters node in the Neo4j database. Expected type: string or integer depending on the database schema.

properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_PARAMETERS relationship. Keys should be valid property names (strings), and values can be any Neo4j-compatible data type (strings, numbers, booleans, etc.). Default is None, meaning no additional properties will be set on the relationship.

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, start node, end node, and any properties assigned to it.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

# Assuming run_query function is defined and Neo4j connection is established
# Example 1: Create relationship without properties
result = create_references_parameters_relationship(
    source_id="test_param_001",
    target_id="param_001"
)

# Example 2: Create relationship with properties
result = create_references_parameters_relationship(
    source_id="test_param_002",
    target_id="param_002",
    properties={
        "created_date": "2024-01-15",
        "created_by": "admin",
        "version": 1
    }
)

if result:
    print(f"Relationship created successfully: {result}")
else:
    print("Failed to create relationship")

Best Practices

  • Always verify that both source and target nodes 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 in calling code to detect failed relationship creation
  • Consider wrapping this function call in a try-except block to handle potential Neo4j connection errors
  • Use transactions when creating multiple relationships to ensure data consistency
  • Be aware that this function will create duplicate relationships if called multiple times with the same source_id and target_id - consider adding MERGE logic if uniqueness is required
  • Ensure the run_query function properly handles parameter sanitization to prevent Cypher injection attacks

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_parameters_relationship 95.2% 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_testparameters_relationship 95.2% similar

    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.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_tests_relationship_v1 92.9% 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_samples_relationship_v1 84.8% 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
  • function create_references_tests_relationship 84.0% similar

    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.

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