xiaohongshu
xiaohongshu
erpnext-database
Database operations and ORM patterns for ERPNext/Frappe v14-v16
Full skill instructions
Frappe provides three abstraction levels for database operations:
| Level | API | Usage |
|---|---|---|
| High-level ORM | frappe.get_doc, frappe.new_doc | Document CRUD with validations |
| Mid-level Query | frappe.db.get_list, frappe.db.get_value | Reading with filters |
| Low-level SQL | frappe.db.sql, frappe.qb | Complex queries, reports |
RULE: Always use the highest abstraction level appropriate for your use case.
What do you want to do?
│
├─ Create/modify/delete document?
│ └─ frappe.get_doc() + .insert()/.save()/.delete()
│
├─ Get single document?
│ ├─ Changes frequently? → frappe.get_doc()
│ └─ Changes rarely? → frappe.get_cached_doc()
│
├─ List of documents?
│ ├─ With user permissions? → frappe.db.get_list()
│ └─ Without permissions? → frappe.get_all()
│
├─ Single field value?
│ ├─ Regular DocType → frappe.db.get_value()
│ └─ Single DocType → frappe.db.get_single_value()
│
├─ Direct update without triggers?
│ └─ frappe.db.set_value() or doc.db_set()
│
└─ Complex query with JOINs?
└─ frappe.qb (Query Builder) or frappe.db.sql()
# With ORM (triggers validations)
doc = frappe.get_doc('Sales Invoice', 'SINV-00001')
# Cached (faster for frequently accessed docs)
doc = frappe.get_cached_doc('Company', 'My Company')
# With user permissions
tasks = frappe.db.get_list('Task',
filters={'status': 'Open'},
fields=['name', 'subject'],
order_by='creation desc',
page_length=50
)
# Without permissions
all_tasks = frappe.get_all('Task', filters={'status': 'Open'})
# Single field
status = frappe.db.get_value('Task', 'TASK001', 'status')
# Multiple fields
subject, status = frappe.db.get_value('Task', 'TASK001', ['subject', 'status'])
# As dict
data = frappe.db.get_value('Task', 'TASK001', ['subject', 'status'], as_dict=True)
doc = frappe.get_doc({
'doctype': 'Task',
'subject': 'New Task',
'status': 'Open'
})
doc.insert()
# Via ORM (with validations)
doc = frappe.get_doc('Task', 'TASK001')
doc.status = 'Completed'
doc.save()
# Direct (without validations) - use carefully!
frappe.db.set_value('Task', 'TASK001', 'status', 'Completed')
{'status': 'Open'} # =
{'status': ['!=', 'Cancelled']} # !=
{'amount': ['>', 1000]} # >
{'amount': ['>=', 1000]} # >=
{'status': ['in', ['Open', 'Working']]} # IN
{'date': ['between', ['2024-01-01', '2024-12-31']]} # BETWEEN
{'subject': ['like', '%urgent%']} # LIKE
{'description': ['is', 'set']} # IS NOT NULL
{'description': ['is', 'not set']} # IS NULL
Task = frappe.qb.DocType('Task')
results = (
frappe.qb.from_(Task)
.select(Task.name, Task.subject)
.where(Task.status == 'Open')
.orderby(Task.creation, order='desc')
.limit(10)
).run(as_dict=True)
SI = frappe.qb.DocType('Sales Invoice')
Customer = frappe.qb.DocType('Customer')
results = (
frappe.qb.from_(SI)
.inner_join(Customer)
.on(SI.customer == Customer.name)
.select(SI.name, Customer.customer_name)
.where(SI.docstatus == 1)
).run(as_dict=True)
# Set/Get
frappe.cache.set_value('key', 'value')
value = frappe.cache.get_value('key')
# With expiry
frappe.cache.set_value('key', 'value', expires_in_sec=3600)
# Delete
frappe.cache.delete_value('key')
from frappe.utils.caching import redis_cache
@redis_cache(ttl=300) # 5 minutes
def get_dashboard_data(user):
return expensive_calculation(user)
# Invalidate cache
get_dashboard_data.clear_cache()
Framework manages transactions automatically:
| Context | Commit | Rollback |
|---|---|---|
| POST/PUT request | After success | On exception |
| Background job | After success | On exception |
frappe.db.savepoint('my_savepoint')
try:
# operations
frappe.db.commit()
except:
frappe.db.rollback(save_point='my_savepoint')
# ❌ SQL Injection risk!
frappe.db.sql(f"SELECT * FROM `tabUser` WHERE name = '{user_input}'")
# ✅ Parameterized
frappe.db.sql("SELECT * FROM `tabUser` WHERE name = %(name)s", {'name': user_input})
# ❌ WRONG
def validate(self):
frappe.db.commit() # Never do this!
# ✅ Framework handles commits
# ✅ Always limit
docs = frappe.get_all('Sales Invoice', page_length=100)
# ❌ N+1 problem
for name in names:
doc = frappe.get_doc('Customer', name)
# ✅ Batch fetch
docs = frappe.get_all('Customer', filters={'name': ['in', names]})
| Feature | v14 | v15 | v16 |
|---|---|---|---|
| Transaction hooks | ❌ | ✅ | ✅ |
| bulk_update | ❌ | ✅ | ✅ |
| Aggregate syntax | String | String | Dict |
# v14/v15
fields=['count(name) as count']
# v16
fields=[{'COUNT': 'name', 'as': 'count'}]
See the references/ folder for detailed documentation:
| Action | Method |
|---|---|
| Get document | frappe.get_doc(doctype, name) |
| Cached document | frappe.get_cached_doc(doctype, name) |
| New document | frappe.new_doc(doctype) or frappe.get_doc({...}) |
| Save document | doc.save() |
| Insert document | doc.insert() |
| Delete document | doc.delete() or frappe.delete_doc() |
| Get list | frappe.db.get_list() / frappe.get_all() |
| Single value | frappe.db.get_value() |
| Single value | frappe.db.get_single_value() |
| Direct update | frappe.db.set_value() / doc.db_set() |
| Exists check | frappe.db.exists() |
| Count records | frappe.db.count() |
| Raw SQL | frappe.db.sql() |
| Query Builder | frappe.qb.from_() |
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience