Document SQL view usage patterns in ERPNext codebase#49642
Closed
Document SQL view usage patterns in ERPNext codebase#49642
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 VIEWstatements). Instead, it employs several view-like constructs and patterns:1. Temporary Tables as Materialized Views
erpnext/accounts/report/accounts_receivable/accounts_receivable.py_ar_voucher_balanceand_ar_ple_rowthat function as materialized views2. Common Table Expressions (CTEs)
erpnext/stock/stock_ledger.pyusesWITH batch_ledger AS (...)for batch quantity calculationsWITH last_balances AS (...)for balance verification queries3. Subquery-based Virtual Views
ROW_NUMBER() OVER()andPARTITION BY4. UNION-based Consolidated Views
erpnext/setup/doctype/company/company.pyuses UNION ALL queries5. Application-Layer Views
6. SQL Procedures and Functions
ar_genkey()encapsulate view-like logicArchitecture Pattern
ERPNext favors application-layer data abstraction over traditional database views, achieving similar functionality through:
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.