function create_references_product_relationship
Creates a REFERENCES_PRODUCT relationship in a Neo4j graph database between a dbo_Treatments node and a dbo_Product node, with optional properties on the relationship.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
2226 - 2246
moderate
Purpose
This function establishes a directed relationship in Neo4j from a treatment record to a product record, allowing the database to track which treatments reference which products. It supports adding custom properties to the relationship edge for storing additional metadata about the reference connection. This is useful in healthcare or pharmaceutical systems where treatments need to be linked to specific products they utilize.
Source Code
def create_references_product_relationship(source_id, target_id, properties=None):
"""Create a REFERENCES_PRODUCT relationship from dbo_Treatments to dbo_Product"""
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_Treatments {id: $source_id})
MATCH (target:dbo_Product {id: $target_id})
CREATE (source)-[r:REFERENCES_PRODUCT]->(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_Treatments node. This should be a value that matches the 'id' property of an existing Treatments node in the Neo4j database.
target_id: The unique identifier of the target dbo_Product node. This should be a value that matches the 'id' property of an existing Product node in the Neo4j database.
properties: Optional dictionary containing key-value pairs to set as properties on the REFERENCES_PRODUCT relationship. Keys should be valid property names and values can be any Neo4j-compatible data type (strings, numbers, booleans, etc.). 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 metadata like relationship type, properties, and connected node references.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query is defined elsewhere
def run_query(query, params):
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
with driver.session() as session:
result = session.run(query, params)
return [record['r'] for record in result]
driver.close()
# Create a simple relationship without properties
relationship = create_references_product_relationship(
source_id='treatment_123',
target_id='product_456'
)
# Create a relationship with properties
relationship_with_props = create_references_product_relationship(
source_id='treatment_123',
target_id='product_456',
properties={
'dosage': '500mg',
'frequency': 'twice daily',
'created_date': '2024-01-15'
}
)
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 that source_id and target_id are not None or empty before passing to the function
- Use consistent property naming conventions when passing the properties dictionary
- Handle the None return value appropriately to detect failed relationship creation
- Consider wrapping this function call in try-except blocks to handle Neo4j connection errors
- Be aware of potential Cypher injection if source_id or target_id come from untrusted sources (though parameterized queries provide some protection)
- Ensure the run_query function properly manages Neo4j driver sessions and connections
- Consider adding duplicate relationship checks if your use case requires unique relationships between nodes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_establishment_relationship_v6 84.8% similar
-
function create_references_tnv_relationship 83.1% similar
-
function get_dbo_treatments_with_references_product_dbo_product 82.7% similar
-
function create_references_houses_relationship_v3 82.2% similar
-
function create_references_flocks_relationship_v2 76.3% similar