function create_references_houses_relationship_v1
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.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1714 - 1734
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database from a source ConceptHouses node to a target Houses node. It's designed for linking conceptual house data to actual house records, allowing optional metadata to be stored as relationship properties. The function uses parameterized Cypher queries to prevent injection attacks and returns the created relationship.
Source Code
def create_references_houses_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_HOUSES relationship from dbo_ConceptHouses 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_ConceptHouses {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_ConceptHouses node. Expected to be a value that matches the 'id' property of a ConceptHouses node in the database (typically 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 (typically string or integer).
properties: Optional dictionary containing key-value pairs to be set as properties on the REFERENCES_HOUSES relationship. Keys should be valid property names (strings), and values can be any Neo4j-compatible data type. 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 fails or no relationship is created (e.g., if source or target nodes don't exist). The return type is typically a Neo4j Relationship object or None.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is already defined and configured
# Example 1: Create relationship without properties
result = create_references_houses_relationship(
source_id="concept_123",
target_id="house_456"
)
# Example 2: Create relationship with properties
result = create_references_houses_relationship(
source_id="concept_123",
target_id="house_456",
properties={
"reference_type": "primary",
"confidence_score": 0.95,
"created_date": "2024-01-15"
}
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship")
Best Practices
- Always verify that both source_id and target_id exist in the database before calling this function to avoid silent failures
- Use meaningful property names in the properties dictionary that follow your database schema conventions
- Handle the None return value appropriately to detect when relationship creation fails
- Consider wrapping this function in a try-except block to handle Neo4j connection errors
- Validate property values before passing them to ensure they are Neo4j-compatible types
- Be aware that this function will create duplicate relationships if called multiple times with the same source and target IDs - consider adding MERGE logic if uniqueness is required
- The function depends on an external run_query function which must be properly configured with Neo4j credentials
- Ensure proper indexing on the 'id' properties of both node types for optimal query performance
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_concepts_relationship 92.6% similar
-
function create_references_houses_relationship_v3 90.3% similar
-
function create_references_houses_relationship 87.6% similar
-
function create_references_establishment_relationship_v2 86.5% similar
-
function create_references_houses_relationship_v2 85.9% similar