🔍 Code Extractor

function create_references_tnv_relationship

Maturity: 45

Creates a REFERENCES_TNV relationship in a Neo4j graph database between a dbo_Treatments node and a dbo_TNV node, with optional properties on the relationship.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
2258 - 2278
Complexity:
moderate

Purpose

This function establishes a directed relationship from a Treatments entity to a TNV (likely Treatment Name Value or similar) entity in a Neo4j database. It allows for dynamic property assignment on the relationship edge, making it useful for linking treatment records to their corresponding TNV references with metadata. The function is part of a graph database schema where treatments reference TNV entities.

Source Code

def create_references_tnv_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_TNV relationship from dbo_Treatments to dbo_TNV"""
    props = ""
    if properties:
        props_list = ', '.join([f"r.{prop} = ${prop}" for prop in properties.keys()])
        props = f"SET {props_list}"
    
    query = f"""
    MATCH (source:dbo_Treatments {id: $source_id})
    MATCH (target:dbo_TNV {id: $target_id})
    CREATE (source)-[r:REFERENCES_TNV]->(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 node (dbo_Treatments). This should be a value that matches the 'id' property of a Treatments node in the Neo4j database. Expected type: string or integer depending on the database schema.

target_id: The unique identifier of the target node (dbo_TNV). This should be a value that matches the 'id' property of a TNV node in the Neo4j database. Expected type: string or integer depending on the 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 result is returned. The relationship object typically includes relationship type, properties, and references to connected nodes.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

# Assuming run_query function is available and Neo4j is configured
# Example 1: Create relationship without properties
result = create_references_tnv_relationship(
    source_id=123,
    target_id=456
)

# Example 2: Create relationship with properties
result = create_references_tnv_relationship(
    source_id=123,
    target_id=456,
    properties={
        'reference_type': 'primary',
        'confidence_score': 0.95,
        'created_date': '2024-01-15'
    }
)

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 creating dangling relationships
  • Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, valid characters)
  • Consider wrapping this function call in error handling to catch Neo4j connection or query execution errors
  • The function uses string interpolation for the Cypher query which could be vulnerable if properties keys are not sanitized; ensure property keys come from trusted sources
  • Check the return value for None to handle cases where nodes don't exist or the relationship creation fails
  • Consider adding transaction management if this function is part of a larger batch operation
  • The run_query function dependency must be properly implemented with connection pooling and error handling

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_dbo_treatments_with_references_tnv_dbo_tnv 84.2% similar

    Queries a Neo4j graph database to retrieve dbo_TNV nodes that are connected to a specific dbo_Treatments node through a REFERENCES_TNV relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_establishment_relationship_v3 83.8% similar

    Creates a REFERENCES_ESTABLISHMENT relationship in a Neo4j graph database between a dbo_TNV 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_establishment_relationship_v6 83.2% similar

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

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_product_relationship 83.1% similar

    Creates a REFERENCES_PRODUCT relationship in a Neo4j graph database between a dbo_Treatments node and a dbo_Product node, with optional properties on the relationship.

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