function create_reference_municipalities
Creates a new Reference_Municipalities node in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
634 - 643
simple
Purpose
This function is designed to insert a new node of type 'Reference_Municipalities' into a Neo4j graph database. It dynamically constructs a Cypher CREATE query based on the provided properties dictionary, executes the query using a run_query helper function, and returns the newly created node. This is useful for populating a graph database with municipality reference data in applications dealing with geographic or administrative data.
Source Code
def create_reference_municipalities(properties):
"""Create a new Reference_Municipalities node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:Reference_Municipalities)
SET {props_list}
RETURN n
"""
result = run_query(query, properties)
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
properties |
- | - | positional_or_keyword |
Parameter Details
properties: A dictionary containing key-value pairs representing the properties to be set on the new Reference_Municipalities node. Keys should be valid property names (strings) and values can be any data type supported by Neo4j (strings, numbers, booleans, lists, etc.). Example: {'name': 'Springfield', 'population': 50000, 'state': 'Illinois'}
Return Value
Returns the first element from the query result (the created node object) if the query execution is successful and returns results. Returns None if the result is empty or the query fails. The returned node object typically contains all properties set during creation along with Neo4j metadata like node ID.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is defined and Neo4j connection is configured
# Example run_query implementation:
# 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['n'] for record in result]
# driver.close()
properties = {
'name': 'Springfield',
'population': 50000,
'state': 'Illinois',
'zip_code': '62701'
}
node = create_reference_municipalities(properties)
if node:
print(f'Created municipality node: {node}')
else:
print('Failed to create node')
Best Practices
- Ensure the properties dictionary contains valid data types supported by Neo4j
- Validate property keys to avoid Cypher injection vulnerabilities (property names should be sanitized)
- The run_query function should handle database connection management and error handling
- Consider adding error handling around the function call to catch database connection issues
- Use parameterized queries (as this function does) to prevent injection attacks
- Ensure unique constraints or indexes are defined on the database if duplicate nodes should be prevented
- Consider using MERGE instead of CREATE if you want to avoid duplicate nodes based on certain properties
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_references_municipalities_relationship 80.3% similar
-
function get_reference_municipalities_by_id 74.4% similar
-
function get_all_reference_municipalities 73.8% similar
-
function get_reference_municipalities_by_uid 71.7% similar
-
function get_dbo_establishment_with_references_municipalities_reference_municipalities 70.8% similar