🔍 Code Extractor

function create_references_houses_relationship

Maturity: 47

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

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1554 - 1574
Complexity:
simple

Purpose

This function establishes a directed relationship in Neo4j from a LIMS_Samples node to a dbo_Houses node. It's designed for linking laboratory information management system (LIMS) samples to housing data, allowing optional metadata to be stored as relationship properties. The function uses Cypher query language to match existing nodes by their IDs and create the relationship between them.

Source Code

def create_references_houses_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_HOUSES relationship from LIMS_Samples to dbo_Houses"""
    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:dbo_Houses {id: $target_id})
    CREATE (source)-[r:REFERENCES_HOUSES]->(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. Type: typically string or integer depending on database schema.

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

properties: Optional dictionary of 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 type (strings, numbers, booleans, etc.). Default is None, meaning no additional properties are 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 relationship is created (e.g., if source or target nodes don't exist). The return type is typically a Neo4j Record object or None.

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_houses_relationship(
    source_id="SAMPLE_001",
    target_id="HOUSE_123"
)

# Example 2: Create relationship with properties
result = create_references_houses_relationship(
    source_id="SAMPLE_002",
    target_id="HOUSE_456",
    properties={
        "reference_date": "2024-01-15",
        "confidence_score": 0.95,
        "verified": True
    }
)

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 returning None
  • Validate that source_id and target_id are not None or empty before calling
  • Use consistent data types for IDs throughout your application (all strings or all integers)
  • Consider wrapping this function in error handling to catch Neo4j connection errors or query failures
  • Be aware that this function does not check for existing relationships - calling it multiple times will create duplicate relationships
  • The properties dictionary keys should follow Neo4j property naming conventions (no spaces, valid identifiers)
  • Ensure the run_query function properly handles Neo4j sessions and transactions
  • Consider adding relationship uniqueness constraints in Neo4j if duplicate relationships should be prevented

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_houses_relationship_v1 87.6% similar

    Creates a REFERENCES_HOUSES relationship in a Neo4j graph database between a dbo_ConceptHouses node and a dbo_Houses node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_houses_relationship_v3 84.6% similar

    Creates a REFERENCES_HOUSES relationship in a Neo4j graph database between a dbo_Treatments node (source) and a dbo_Houses node (target), with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_samples_relationship 84.4% 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_establishment_relationship_v1 83.8% similar

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

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_samples_relationship_v1 83.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
← Back to Browse