🔍 Code Extractor

function create_references_flocktypes_relationship

Maturity: 47

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.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1778 - 1798
Complexity:
simple

Purpose

This function establishes a directed relationship in Neo4j from an Establishment entity to a FlockTypes entity. It's designed for graph database operations where establishments need to reference specific flock types, allowing for optional metadata to be stored on the relationship itself. The function constructs and executes a Cypher query to create this relationship and returns the created relationship object.

Source Code

def create_references_flocktypes_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_FLOCKTYPES relationship from dbo_Establishment to dbo_FlockTypes"""
    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_Establishment {id: $source_id})
    MATCH (target:dbo_FlockTypes {id: $target_id})
    CREATE (source)-[r:REFERENCES_FLOCKTYPES]->(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 dbo_Establishment node. This should match the 'id' property of an existing Establishment node in the Neo4j database. Expected type: string or integer depending on your database schema.

target_id: The unique identifier of the target dbo_FlockTypes node. This should match the 'id' property of an existing FlockTypes node in the Neo4j database. Expected type: string or integer depending on your database schema.

properties: Optional dictionary containing key-value pairs to be 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 will be set on the relationship.

Return Value

Returns the created relationship object (r) from Neo4j if successful, containing all relationship properties and metadata. Returns None if the query fails to create the relationship or if no result is returned (e.g., if source or target nodes don't exist). The relationship object is the first element from the result set.

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
relationship = create_references_flocktypes_relationship(
    source_id="EST123",
    target_id="FLOCK456"
)

# Example 2: Create relationship with properties
relationship = create_references_flocktypes_relationship(
    source_id="EST123",
    target_id="FLOCK456",
    properties={
        "created_date": "2024-01-15",
        "reference_type": "primary",
        "active": True
    }
)

if relationship:
    print(f"Relationship created successfully: {relationship}")
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 source_id and target_id before passing them to prevent injection attacks or invalid queries
  • Use consistent data types for IDs throughout your application (either all strings or all integers)
  • Consider wrapping this function call in try-except blocks to handle potential Neo4j connection errors
  • The properties dictionary keys should follow Neo4j property naming conventions (no spaces, valid characters)
  • Be aware that this function does not check for existing relationships - it will create duplicate relationships if called multiple times with the same IDs
  • Consider adding relationship uniqueness constraints in Neo4j if duplicate relationships should be prevented
  • The run_query function dependency must be properly implemented with error handling and connection management

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_flocktypes_relationship_v2 93.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 92.4% 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_flocktypes_relationship_v1 91.3% similar

    Creates a REFERENCES_FLOCKTYPES relationship in a Neo4j graph database between a dbo_Houses 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_flocks_relationship_v2 85.9% 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 get_dbo_establishment_with_references_flocktypes_dbo_flocktypes 85.2% similar

    Queries a Neo4j graph database to retrieve dbo_FlockTypes nodes that are connected to a specific dbo_Establishment node through a REFERENCES_FLOCKTYPES relationship.

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