Skip to content

Document SQL view usage patterns in ERPNext codebase#49642

Closed
Copilot wants to merge 1 commit intodevelopfrom
copilot/fix-f5fa79a5-0136-4887-a8f7-6513e994e890
Closed

Document SQL view usage patterns in ERPNext codebase#49642
Copilot wants to merge 1 commit intodevelopfrom
copilot/fix-f5fa79a5-0136-4887-a8f7-6513e994e890

Conversation

Copy link

Copilot AI commented Sep 19, 2025

This PR provides a comprehensive analysis of SQL view utilization patterns in the ERPNext codebase in response to the question "Find out where SQL views are utilized in the codebase?"

Key Findings

ERPNext does not use traditional SQL database views (CREATE VIEW statements). Instead, it employs several view-like constructs and patterns:

1. Temporary Tables as Materialized Views

  • Location: erpnext/accounts/report/accounts_receivable/accounts_receivable.py
  • Creates temporary tables _ar_voucher_balance and _ar_ple_row that function as materialized views
  • Used with stored procedures for complex financial calculations in accounts receivable/payable reports

2. Common Table Expressions (CTEs)

  • Stock Ledger: erpnext/stock/stock_ledger.py uses WITH batch_ledger AS (...) for batch quantity calculations
  • Stock Balance Tests: Uses WITH last_balances AS (...) for balance verification queries
  • Provides temporary named result sets for complex analytical queries

3. Subquery-based Virtual Views

  • Bank Transactions: Complex windowed subqueries with ROW_NUMBER() OVER() and PARTITION BY
  • Reports: Multiple report files use inline subqueries as virtual views
  • Advanced SQL window functions for analytical data processing

4. UNION-based Consolidated Views

  • Company Setup: erpnext/setup/doctype/company/company.py uses UNION ALL queries
  • Combines data from multiple transaction tables (Quotation, Sales Order, Delivery Note) into consolidated views

5. Application-Layer Views

  • Python reports act as "views" through dynamic SQL generation
  • Frappe's query builder provides view-like functionality at the application layer
  • Business logic encapsulation through report-based data access patterns

6. SQL Procedures and Functions

  • Database functions like ar_genkey() encapsulate view-like logic
  • Stored procedures provide complex business rule implementations

Architecture Pattern

ERPNext favors application-layer data abstraction over traditional database views, achieving similar functionality through:

  • Temporary tables for materialized view behavior
  • CTEs for hierarchical and analytical queries
  • Dynamic query generation for flexible data presentation
  • Report-based virtual views with business logic integration

This approach provides greater flexibility and maintainability while preserving the benefits of view-like data access patterns.

Documentation

Complete analysis with code examples and patterns is documented in the analysis file, providing a reference for developers working with ERPNext's data access patterns.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@gavindsouza gavindsouza deleted the copilot/fix-f5fa79a5-0136-4887-a8f7-6513e994e890 branch September 19, 2025 17:49
Copilot AI restored the copilot/fix-f5fa79a5-0136-4887-a8f7-6513e994e890 branch September 19, 2025 17:54
Copilot AI changed the title [WIP] Find out where sql views are utilized in the codebase? Document SQL view usage patterns in ERPNext codebase Sep 19, 2025
Copilot AI requested a review from gavindsouza September 19, 2025 17:58
@gavindsouza gavindsouza deleted the copilot/fix-f5fa79a5-0136-4887-a8f7-6513e994e890 branch September 19, 2025 18:03
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants