🔍 Code Extractor

function create_references_establishment_relationship_v5

Maturity: 45

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.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1874 - 1894
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j graph database to link flock records to establishment records. It's designed for data modeling scenarios where flocks need to reference their associated establishments. The function supports adding custom properties to the relationship edge, making it flexible for storing metadata about the reference (e.g., timestamps, reference types, or other contextual information).

Source Code

def create_references_establishment_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_ESTABLISHMENT relationship from dbo_Flocks 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_Flocks {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_Flocks). This should match the 'id' property of an existing dbo_Flocks node in the Neo4j database. Expected type: string or integer depending on your database schema.

target_id: The unique identifier of the target node (dbo_Establishment). This should match the 'id' property of an existing dbo_Establishment 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, dates, 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 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

# Example 1: Create relationship without properties
result = create_references_establishment_relationship(
    source_id="flock_123",
    target_id="establishment_456"
)

# Example 2: Create relationship with properties
result = create_references_establishment_relationship(
    source_id="flock_123",
    target_id="establishment_456",
    properties={
        "reference_date": "2024-01-15",
        "reference_type": "primary",
        "confidence_score": 0.95
    }
)

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

Best Practices

  • Ensure both source_id and target_id correspond to existing nodes 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)
  • Consider wrapping this function call in try-except blocks to handle potential Neo4j connection errors or query failures
  • Be aware of potential Cypher injection vulnerabilities: the function uses parameterized queries for IDs but constructs property names dynamically. Only pass trusted property names
  • Check the return value for None to determine if the relationship was successfully created
  • Consider adding uniqueness constraints or checking for existing relationships before creating new ones to avoid duplicates
  • The function assumes the run_query function handles database connection management and error handling appropriately

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_flocktypes_relationship 92.4% similar

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

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_flocktypes_relationship_v2 87.4% similar

    Creates a directed REFERENCES_FLOCKTYPES relationship in a Neo4j graph database from a dbo_Flocks node to a dbo_FlockTypes node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_flocks_relationship_v2 87.4% similar

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

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_establishment_relationship_v4 87.0% similar

    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.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_houses_relationship_v2 86.3% similar

    Creates a directed REFERENCES_HOUSES relationship in a Neo4j graph database from a dbo_Flocks node to a dbo_Houses node, with optional properties on the relationship.

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