🔍 Code Extractor

function create_references_requests_relationship

Maturity: 47

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

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1426 - 1446
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j graph database from a source LIMS_Samples node to a target LIMS_Requests node. It's designed for Laboratory Information Management Systems (LIMS) to track which samples reference which requests. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.

Source Code

def create_references_requests_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_REQUESTS relationship from LIMS_Samples to LIMS_Requests"""
    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_Samples {id: $source_id})
    MATCH (target:LIMS_Requests {id: $target_id})
    CREATE (source)-[r:REFERENCES_REQUESTS]->(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_Samples node. This should be a value that matches the 'id' property of an existing LIMS_Samples node in the Neo4j database. Expected type: string or integer depending on your database schema.

target_id: The unique identifier of the target LIMS_Requests node. This should be a value that matches the 'id' property of an existing LIMS_Requests 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.). 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 fails or no result is returned (e.g., if source or target nodes don't exist). The relationship object typically includes metadata like relationship type, start/end node references, and all properties.

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
result = create_references_requests_relationship(
    source_id="SAMPLE_001",
    target_id="REQUEST_123"
)

# Create a relationship with properties
relationship_props = {
    "created_date": "2024-01-15",
    "created_by": "lab_technician_42",
    "priority": "high"
}

result = create_references_requests_relationship(
    source_id="SAMPLE_002",
    target_id="REQUEST_124",
    properties=relationship_props
)

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 before calling this function to avoid silent failures
  • Check the return value to confirm the relationship was created successfully
  • Use consistent data types for source_id and target_id that match your database schema
  • Validate property keys and values before passing them to avoid Cypher syntax errors
  • Consider wrapping this function in a try-except block to handle potential Neo4j connection or query errors
  • The function uses parameterized queries which is good for security, but ensure the run_query function properly implements parameter binding
  • Be aware that this function will fail silently (return None) if nodes don't exist rather than raising an exception
  • Consider adding duplicate relationship checks if your use case requires unique relationships between nodes

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_samples_relationship 91.0% 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 89.9% 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_sampletypes_relationship_v1 87.2% similar

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

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_tests_relationship 86.4% 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
  • function create_references_establishment_relationship 84.7% similar

    Creates a REFERENCES_ESTABLISHMENT relationship in a Neo4j graph database between a LIMS_Requests node and a dbo_Establishment node, with optional properties on the relationship.

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