function create_references_flocktypes_relationship_v2
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.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1906 - 1926
moderate
Purpose
This function establishes a typed relationship between flock instances and their corresponding flock types in a Neo4j database. It's used to model the reference/classification relationship where individual flocks point to their type definitions. The function supports adding custom properties to the relationship edge for storing metadata about the reference.
Source Code
def create_references_flocktypes_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_FLOCKTYPES relationship from dbo_Flocks 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_Flocks {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 node (dbo_Flocks). This value is used to match the specific flock instance that will be the origin of the relationship. Expected to be a string or integer that corresponds to the 'id' property of a dbo_Flocks node.
target_id: The unique identifier of the target node (dbo_FlockTypes). This value is used to match the specific flock type that will be the destination of the relationship. Expected to be a string or integer that corresponds to the 'id' property of a dbo_FlockTypes 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.). 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 execution fails or returns no results. The relationship object typically includes metadata like relationship type, start/end node references, and all assigned properties.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is defined and Neo4j is connected
# Example 1: Create relationship without properties
result = create_references_flocktypes_relationship(
source_id=123,
target_id=456
)
# Example 2: Create relationship with properties
result = create_references_flocktypes_relationship(
source_id=123,
target_id=456,
properties={
'created_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 to detect when relationship creation fails
- Consider wrapping this function in a try-except block to handle Neo4j connection errors or query execution failures
- Be aware of potential Cypher injection vulnerabilities if source_id or target_id come from untrusted sources; the parameterized query approach used here helps mitigate this
- Ensure the run_query function properly manages database connections and transactions
- Consider adding uniqueness constraints or checking for existing relationships before creating duplicates if business logic requires it
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_flocktypes_relationship_v1 94.1% similar
-
function create_references_flocktypes_relationship 93.6% similar
-
function create_references_flocks_relationship_v2 90.0% similar
-
function create_references_establishment_relationship_v5 87.4% similar
-
function create_references_flocks_relationship_v1 86.6% similar