🔍 Code Extractor

function create_references_houses_relationship_v2

Maturity: 47

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.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1938 - 1958
Complexity:
moderate

Purpose

This function establishes a relationship between flock and house entities in a Neo4j database, likely representing a poultry or livestock management system where flocks are associated with specific housing structures. It allows for flexible property assignment on the relationship edge and uses parameterized queries to prevent injection attacks.

Source Code

def create_references_houses_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_HOUSES relationship from dbo_Flocks to dbo_Houses"""
    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_Houses {id: $target_id})
    CREATE (source)-[r:REFERENCES_HOUSES]->(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_Flocks node. Expected to be a value that matches the 'id' property of a Flocks node in the database (likely string or integer).

target_id: The unique identifier of the target dbo_Houses node. Expected to be a value that matches the 'id' property of a Houses node in the database (likely string or integer).

properties: Optional dictionary containing key-value pairs to set as properties on the created relationship. Keys should be valid property names, 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 set properties. Returns None if the query execution fails or returns no results. The relationship object is typically a Neo4j Record containing relationship metadata.

Dependencies

  • neo4j

Required Imports

from neo4j import GraphDatabase

Usage Example

# Assuming run_query function is available and Neo4j is configured

# Create relationship without properties
result = create_references_houses_relationship(
    source_id="flock_001",
    target_id="house_042"
)

# Create relationship with properties
result = create_references_houses_relationship(
    source_id="flock_001",
    target_id="house_042",
    properties={
        "assigned_date": "2024-01-15",
        "capacity_percentage": 85.5,
        "is_active": True
    }
)

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

Best Practices

  • Ensure both source and target nodes exist in the database before calling this function to avoid silent failures
  • Validate source_id and target_id before passing to prevent query errors
  • Use properties parameter to add metadata like timestamps, status flags, or relationship-specific attributes
  • Handle the None return value to detect when relationship creation fails
  • Consider wrapping this function in a try-except block to handle Neo4j connection errors
  • The function uses parameterized queries which is good for security, but ensure the run_query function properly executes them
  • Be aware that this creates a new relationship each time - it does not check for existing relationships, which could lead to duplicates
  • Property keys in the properties dictionary should follow Neo4j naming conventions (alphanumeric, underscores)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_flocktypes_relationship_v1 91.9% 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_establishment_relationship_v5 86.3% 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_houses_relationship_v1 85.9% similar

    Creates a REFERENCES_HOUSES relationship in a Neo4j graph database between a dbo_ConceptHouses node and a dbo_Houses node, with optional properties on the relationship.

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