function create_references_medicationtypes_relationship
Creates a REFERENCES_MEDICATIONTYPES relationship in a Neo4j graph database between a Parameter_MedicationTypeProperties node and a Parameter_MedicationTypes node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1650 - 1670
moderate
Purpose
This function establishes a directed relationship in a Neo4j graph database to link medication type properties to their corresponding medication types. It's designed for healthcare or pharmaceutical data modeling where medication type properties need to reference their parent medication type entities. The function supports adding custom properties to the relationship edge for additional metadata.
Source Code
def create_references_medicationtypes_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_MEDICATIONTYPES relationship from Parameter_MedicationTypeProperties to Parameter_MedicationTypes"""
props = ""
if properties:
props_list = ', '.join([f"r.{prop} = ${prop}" for prop in properties.keys()])
props = f"SET {props_list}"
query = f"""
MATCH (source:Parameter_MedicationTypeProperties {id: $source_id})
MATCH (target:Parameter_MedicationTypes {id: $target_id})
CREATE (source)-[r:REFERENCES_MEDICATIONTYPES]->(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 (Parameter_MedicationTypeProperties). This value is used to match the source node in the Neo4j database using its 'id' property.
target_id: The unique identifier of the target node (Parameter_MedicationTypes). This value is used to match the target node in the Neo4j database using its 'id' property.
properties: Optional dictionary containing key-value pairs to be set as properties on the created relationship. Keys should be valid property names and values can be any Neo4j-compatible data type. Defaults to None if no additional 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 relationship type, properties, and references to connected nodes.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is defined and Neo4j is configured
# Create relationship without properties
relationship = create_references_medicationtypes_relationship(
source_id="prop_123",
target_id="type_456"
)
# Create relationship with properties
relationship_with_props = create_references_medicationtypes_relationship(
source_id="prop_123",
target_id="type_456",
properties={
"created_date": "2024-01-15",
"confidence_score": 0.95,
"verified": True
}
)
if relationship_with_props:
print("Relationship created successfully")
else:
print("Failed to create relationship")
Best Practices
- Ensure both source and target nodes exist in the database before calling this function to avoid query failures
- Validate source_id and target_id parameters to prevent injection attacks or malformed queries
- Use meaningful property names in the properties dictionary that follow Neo4j naming conventions
- Handle the None return value appropriately to detect when relationship creation fails
- Consider wrapping this function call in try-except blocks to handle Neo4j connection errors
- Avoid creating duplicate relationships by checking if the relationship already exists before calling this function
- Ensure the run_query function properly manages database connections and transactions
- Use parameterized queries (as implemented) to prevent Cypher injection vulnerabilities
- Consider adding validation for property values to ensure they are compatible with Neo4j data types
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_parameter_medicationtypes 81.4% similar
-
function get_parameter_medicationtypeproperties_with_references_medicationtypes_parameter_medicationtypes 80.6% similar
-
function create_parameter_medicationtypeproperties 78.8% similar
-
function create_references_parameters_relationship 77.1% similar
-
function create_references_parameters_relationship_v1 75.7% similar