function create_references_flocktypes_relationship_v1
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.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2002 - 2022
moderate
Purpose
This function establishes a directed relationship in Neo4j from a Houses entity to a FlockTypes entity, representing a reference between house records and flock type classifications. It's designed for building graph database schemas where houses need to reference specific flock types, commonly used in agricultural or livestock management systems. The function supports adding custom properties to the relationship for additional metadata.
Source Code
def create_references_flocktypes_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_FLOCKTYPES relationship from dbo_Houses 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_Houses {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_Houses). This should be a value that matches the 'id' property of an existing Houses node in the Neo4j database. Expected type: string or integer depending on the database schema.
target_id: The unique identifier of the target node (dbo_FlockTypes). This should be a value that matches the 'id' property of an existing FlockTypes node in the Neo4j database. Expected type: string or integer depending on the 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 on 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 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 function is defined and Neo4j is configured
# 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,
'notes': 'Primary flock type reference'
}
)
if result:
print(f"Relationship created successfully: {result}")
else:
print("Failed to create relationship")
Best Practices
- Ensure both source_id and target_id correspond to existing nodes in the database before calling this function to avoid silent failures
- Validate the properties dictionary keys to ensure they follow Neo4j property naming conventions (no spaces, valid characters)
- 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 or query execution failures
- Be aware of the Cypher injection risk: the properties parameter values are parameterized, but ensure property keys are from trusted sources
- Note the syntax error in the query: {id: $source_id} should be {{id: $source_id}} with double curly braces for proper Cypher syntax
- Consider adding transaction management and rollback capabilities for production use
- Verify that the run_query function properly handles connection pooling and session management
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_flocktypes_relationship_v2 94.1% similar
-
function create_references_houses_relationship_v2 91.9% similar
-
function create_references_flocktypes_relationship 91.3% similar
-
function create_references_flocks_relationship_v2 87.0% similar
-
function get_dbo_houses_with_references_flocktypes_dbo_flocktypes 85.4% similar