🔍 Code Extractor

function create_references_municipalities_relationship

Maturity: 47

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

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
1746 - 1766
Complexity:
moderate

Purpose

This function establishes a directed relationship in a Neo4j graph database to link establishment records with municipality reference data. It's designed for data integration scenarios where establishments need to be associated with their corresponding municipalities. The function supports adding custom properties to the relationship edge, making it flexible for storing additional metadata about the reference connection.

Source Code

def create_references_municipalities_relationship(source_id, target_id, properties=None):
    """Create a REFERENCES_MUNICIPALITIES relationship from dbo_Establishment to Reference_Municipalities"""
    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:Reference_Municipalities {id: $target_id})
    CREATE (source)-[r:REFERENCES_MUNICIPALITIES]->(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_Establishment). This value is used to match the establishment node in the Neo4j database. Expected to be a string or integer that corresponds to the 'id' property of a dbo_Establishment node.

target_id: The unique identifier of the target node (Reference_Municipalities). This value is used to match the municipality reference node in the Neo4j database. Expected to be a string or integer that corresponds to the 'id' property of a Reference_Municipalities node.

properties: Optional dictionary containing key-value pairs to be 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, etc.). If None or omitted, no additional properties are added to 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 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 available and Neo4j is configured
# Example 1: Create relationship without properties
result = create_references_municipalities_relationship(
    source_id="EST123",
    target_id="MUN456"
)

# Example 2: Create relationship with properties
result = create_references_municipalities_relationship(
    source_id="EST123",
    target_id="MUN456",
    properties={
        "reference_date": "2024-01-15",
        "confidence_score": 0.95,
        "verified": True
    }
)

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

Best Practices

  • Ensure both source_id and target_id exist in the database before calling this function to avoid creating dangling relationships
  • Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, special characters)
  • Handle the None return value appropriately to detect failed relationship creation
  • Consider wrapping this function in a try-except block to handle Neo4j connection errors or query execution failures
  • Use transactions when creating multiple relationships to ensure data consistency
  • Verify that the run_query function properly handles parameter sanitization to prevent Cypher injection attacks
  • Consider checking if the relationship already exists before creating a new one to avoid duplicate relationships
  • Property values should be Neo4j-compatible types (avoid complex objects that cannot be serialized)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_references_establishment_relationship_v2 85.7% 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 get_dbo_establishment_with_references_municipalities_reference_municipalities 84.2% similar

    Retrieves Reference_Municipalities nodes from a Neo4j graph database that are connected to a specific dbo_Establishment node via a REFERENCES_MUNICIPALITIES relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_establishment_relationship_v4 83.4% 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_establishment_relationship_v3 81.6% 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_reference_municipalities 80.3% similar

    Creates a new Reference_Municipalities node in a Neo4j graph database with the specified properties and returns the created node.

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