function create_references_concepts_relationship
Creates a REFERENCES_CONCEPTS relationship in a Neo4j graph database between a dbo_ConceptHouses node and a dbo_Concepts node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1682 - 1702
moderate
Purpose
This function establishes a directed relationship in Neo4j from a ConceptHouses entity to a Concepts entity. It's designed for building knowledge graphs or data models where concept houses need to reference specific concepts. The function supports adding custom properties to the relationship edge, making it flexible for storing metadata about the reference relationship.
Source Code
def create_references_concepts_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_CONCEPTS relationship from dbo_ConceptHouses to dbo_Concepts"""
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_Concepts {id: $target_id})
CREATE (source)-[r:REFERENCES_CONCEPTS]->(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_ConceptHouses). This should match the 'id' property of an existing ConceptHouses node in the Neo4j database. Expected type: string or integer depending on your database schema.
target_id: The unique identifier of the target node (dbo_Concepts). This should match the 'id' property of an existing Concepts node in the Neo4j database. Expected type: string or integer depending on your database schema.
properties: Optional dictionary containing key-value pairs to 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 are set.
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 Record object or None.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j is configured
# Basic usage without properties
relationship = create_references_concepts_relationship(
source_id="house_123",
target_id="concept_456"
)
# Usage with properties
relationship = create_references_concepts_relationship(
source_id="house_123",
target_id="concept_456",
properties={
"created_at": "2024-01-15",
"confidence": 0.95,
"reference_type": "primary"
}
)
if relationship:
print(f"Relationship created successfully: {relationship}")
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 before passing it to ensure all keys are valid property names and values are Neo4j-compatible types
- Consider wrapping this function call in try-except blocks to handle potential Neo4j connection errors or query failures
- Be aware that this function uses string interpolation for the Cypher query, which could be a security concern if properties keys come from untrusted sources
- The function depends on an external run_query function that must be properly implemented with Neo4j driver connection handling
- Consider adding duplicate relationship checks if your use case requires preventing multiple REFERENCES_CONCEPTS relationships between the same nodes
- Use transactions appropriately if this function is part of a larger batch operation to ensure data consistency
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_houses_relationship_v1 92.6% similar
-
function create_references_establishment_relationship_v2 83.2% similar
-
function get_dbo_concepthouses_with_references_concepts_dbo_concepts 82.5% similar
-
function create_references_houses_relationship_v3 81.4% similar
-
function get_dbo_concepthouses_with_references_houses_dbo_houses 79.6% similar