🔍 Code Extractor

function create_references_flocks_relationship_v1

Maturity: 47

Creates a REFERENCES_FLOCKS relationship in a Neo4j graph database between an InterventionProtocolFlocks node and a Flocks node, with optional properties on the relationship.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
2034 - 2054
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j database from a source node (dbo_InterventionProtocolFlocks) to a target node (dbo_Flocks). It's designed to link intervention protocols to specific flocks, allowing optional metadata to be stored as relationship properties. The function constructs a Cypher query dynamically based on whether properties are provided, executes it, and returns the created relationship.

Source Code

def create_references_flocks_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_FLOCKS relationship from dbo_InterventionProtocolFlocks to dbo_Flocks"""
    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_InterventionProtocolFlocks {id: $source_id})
    MATCH (target:dbo_Flocks {id: $target_id})
    CREATE (source)-[r:REFERENCES_FLOCKS]->(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 (id property) of the source node of type dbo_InterventionProtocolFlocks. This should be a value that matches the 'id' property of an existing InterventionProtocolFlocks node in the database.

target_id: The unique identifier (id property) of the target node of type dbo_Flocks. This should be a value that matches the 'id' property of an existing Flocks node in the database.

properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_FLOCKS relationship. Keys should be valid property names, and values can be any Neo4j-compatible data type (strings, numbers, booleans, etc.). Defaults to None if no properties are needed.

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 execution fails or returns no results. The relationship object typically includes metadata like relationship type, properties, and connected node references.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

# Assuming run_query function is defined and Neo4j is configured

# Create relationship without properties
relationship = create_references_flocks_relationship(
    source_id="protocol_123",
    target_id="flock_456"
)

# Create relationship with properties
relationship_with_props = create_references_flocks_relationship(
    source_id="protocol_123",
    target_id="flock_456",
    properties={
        "created_date": "2024-01-15",
        "priority": "high",
        "active": True
    }
)

if relationship_with_props:
    print("Relationship created successfully")
else:
    print("Failed to create relationship")

Best Practices

  • Ensure both source and target nodes exist in the database before calling this function to avoid query failures
  • Validate that source_id and target_id are not None or empty before passing to the function
  • Use parameterized queries (as implemented) to prevent Cypher injection attacks
  • Handle the None return value appropriately in calling code to detect failed relationship creation
  • Consider wrapping this function call in try-except blocks to handle potential Neo4j connection or query errors
  • Validate property keys and values before passing to ensure they are Neo4j-compatible data types
  • Be aware that this function uses string formatting for the Cypher query, which could be refactored to use more robust query building if the properties logic becomes more complex
  • The function assumes run_query is available in scope - ensure proper import or definition of this dependency

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_interventionprotocols_relationship 91.8% similar

    Creates a directed REFERENCES_INTERVENTIONPROTOCOLS relationship in a Neo4j graph database from a dbo_InterventionProtocolFlocks node to a dbo_InterventionProtocols node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_flocks_relationship_v2 88.7% 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_flocks_relationship 87.1% similar

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

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