🔍 Code Extractor

function create_references_establishment_relationship_v4

Maturity: 45

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

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1842 - 1862
Complexity:
simple

Purpose

This function establishes a directed relationship in Neo4j from an EstablishmentCycles entity to an Establishment entity. It's designed for linking cycle-based establishment data to their corresponding establishment records in a graph database schema. The function supports adding custom properties to the relationship and uses parameterized queries to prevent injection attacks.

Source Code

def create_references_establishment_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_ESTABLISHMENT relationship from dbo_EstablishmentCycles to dbo_Establishment"""
    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_EstablishmentCycles {id: $source_id})
    MATCH (target:dbo_Establishment {id: $target_id})
    CREATE (source)-[r:REFERENCES_ESTABLISHMENT]->(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_EstablishmentCycles). This value is used to match the source node in the Neo4j database using its 'id' property.

target_id: The unique identifier of the target node (dbo_Establishment). This value is used to match the target node in the Neo4j database using its 'id' property.

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 type. Defaults to None if no properties are needed.

Return Value

Returns the first result from the query execution (the created relationship object 'r') if successful, or None if no results are returned. The relationship object contains the properties and metadata of the created REFERENCES_ESTABLISHMENT relationship.

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_establishment_relationship(
    source_id="cycle_123",
    target_id="establishment_456"
)

# Example 2: Create relationship with properties
properties = {
    "created_date": "2024-01-15",
    "status": "active",
    "confidence_score": 0.95
}
result = create_references_establishment_relationship(
    source_id="cycle_789",
    target_id="establishment_012",
    properties=properties
)

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

Best Practices

  • Always validate that source_id and target_id exist in the database before calling this function to avoid creating dangling relationships
  • Use meaningful property names in the properties dictionary that follow your database schema conventions
  • Handle the None return value appropriately to detect when relationship creation fails
  • Consider wrapping this function in a try-except block to handle Neo4j connection errors or query execution failures
  • Ensure the run_query function properly manages database connections and transactions
  • Be aware that this function uses CREATE which will always create a new relationship - use MERGE if you want to avoid duplicates
  • Property values should be Neo4j-compatible types (strings, numbers, booleans, lists, etc.)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_establishment_relationship_v2 92.0% similar

    Creates a REFERENCES_ESTABLISHMENT relationship in a Neo4j graph database between a dbo_Houses 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_v3 90.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 87.4% 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_establishment_relationship_v5 87.0% similar

    Creates a REFERENCES_ESTABLISHMENT relationship in a Neo4j graph database between a dbo_Flocks 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_establishment_relationship 87.0% 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