Some SQL queries useful for exploring links between DocTypes in the UI

Here are some SQL queries useful for exploring links between DocTypes in the UI.

They might help to explore the workflow logic of the enormous system which is ERPNext, for instance for learning more about it, or for checking completeness or prerequisites for an implementation.

Have fun exploring, implementing and building!

Useful SQL queries:
(Note: some have limit clauses in order to extend the returned list beyond 1000 rows.
This seems to be the default limit when opening the mariadb client with “bench mariadb”.
So, check if your own queries return exactly 1000 rows, in that case chances are 1:999 that this default limit limits your query results.)

tabDocFields:

SELECT * FROM tabDocField ORDER BY parent, idx LIMIT 99999;

Forms:

SELECT DISTINCT parent FROM tabDocField;

SELECT DISTINCT parent FROM tabDocField WHERE parent LIKE '%sales%';

SELECT DISTINCT parent FROM tabDocField WHERE parent LIKE '%account%';

All fields of all forms:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' ORDER BY parent, idx LIMIT 99999;

Required fields of all forms:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' AND reqd=1 ORDER BY parent, idx LIMIT 99999;

All link fields of all forms:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' and fieldtype = 'Link' ORDER BY parent, idx LIMIT 99999;

All mandatory link fields of all forms:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' and fieldtype = 'Link' AND reqd=1 ORDER BY parent, idx LIMIT 99999;

Modules:

SELECT DISTINCT module FROM tabDocType;

SELECT module, count(*) FROM tabDocType GROUP BY module ORDER BY module;

Linked account fields:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' AND fieldtype = 'Link' AND options = 'Account' ORDER BY parent, idx LIMIT 99999;

Linked account fields, ordered by fieldname (to see which ones match):

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' AND fieldtype = 'Link' AND options = 'Account' ORDER BY fieldname,parent, idx LIMIT 99999;

Mandatory linked account fields:

SELECT parent, label, fieldname, fieldtype, reqd, options FROM tabDocField WHERE parenttype='DocType' AND fieldtype = 'Link' AND options = 'Account' AND reqd=1 ORDER BY parent, idx LIMIT 99999;

Tables which are linked to by some field:

SELECT options FROM tabDocField WHERE fieldtype='LINK' GROUP BY options ORDER BY options;

Tables which are linked to, with fields linking to them:

SELECT options,parent,label,fieldname FROM tabDocField WHERE fieldtype='LINK' ORDER BY options,parent,label LIMIT 99999;

Some as above, but with more explicit column heads:

SELECT options AS 'A Link field to doc of DocType', parent AS 'is in the Form of DocType', idx AS 'Pos', label AS 'The Field label in the Form is', fieldname AS 'Database field name' FROM tabDocField WHERE fieldtype='LINK' ORDER BY options,parent,idx LIMIT 99999;

Self-referencing DocTypes:

SELECT options,label FROM tabDocField WHERE options=parent ORDER BY label, options;

Dto., with variations regarding amendable DocTypes (get these out of the way, or use this as proxy for submittable DocTypes):

SELECT options,label FROM tabDocField WHERE options=parent AND label!='Amended From' ORDER BY label,options;

SELECT options,label FROM tabDocField WHERE options=parent AND label='Amended From' ORDER BY options;

Dto., candidates for Tree Structure:

SELECT options,label FROM tabDocField WHERE options=parent AND label LIKE 'Parent%' ORDER BY options;

8 Likes

great job!

Here is another one:
(This time with the results to make this post more easy to see the use of it for people not yet into SQL queries.)

List naming series of DocTypes:

This can help to get a better overview on all these DocType series (scroll down a bit to see them).

SELECT fieldname,parent,options AS series FROM tabDocField WHERE fieldname LIKE 'nami_%' ORDER BY series;

+-----------------------+-----------------------------+---------------------------------------------------------------------------------------------------------------------+
| fieldname             | parent                      | series                                                                                                              |
+-----------------------+-----------------------------+---------------------------------------------------------------------------------------------------------------------+
| naming_series_tab     | Document Naming Settings    | NULL                                                                                                                |
| naming_series_prefix  | Stock Settings              | NULL                                                                                                                |
| naming_series         | Budget                      | NULL                                                                                                                |
| naming_section        | Document Naming Rule        | NULL                                                                                                                |
| naming_series         | Journal Entry Template      | NULL                                                                                                                |
| naming_section        | Customize Form              | NULL                                                                                                                |
| naming_series_options | Document Naming Settings    | NULL                                                                                                                |
| naming_rule           | DocType                     | 
Set by user
Autoincrement
By fieldname
By "Naming Series" field
Expression
Expression (old style)
Random
By script |
| naming_rule           | Customize Form              | 
Set by user
By fieldname
By "Naming Series" field
Expression
Expression (old style)
Random
By script               |
| naming_series         | Asset Depreciation Schedule | ACC-ADS-.YYYY.-                                                                                                     |
| naming_series         | Asset Maintenance Log       | ACC-AML-.YYYY.-                                                                                                     |
| naming_series         | Asset Shift Allocation      | ACC-ASA-.YYYY.-                                                                                                     |
| naming_series         | Asset Capitalization        | ACC-ASC-.YYYY.-                                                                                                     |
| naming_series         | Asset Repair                | ACC-ASR-.YYYY.-                                                                                                     |
| naming_series         | Asset                       | ACC-ASS-.YYYY.-                                                                                                     |
| naming_series         | Bank Transaction            | ACC-BTN-.YYYY.-                                                                                                     |
| naming_series         | Journal Entry               | ACC-JV-.YYYY.-                                                                                                      |
| naming_series         | Payment Entry               | ACC-PAY-.YYYY.-                                                                                                     |
| naming_series         | Purchase Invoice            | ACC-PINV-.YYYY.-
ACC-PINV-RET-.YYYY.-                                                                               |
| naming_series         | Payment Request             | ACC-PRQ-.YYYY.-                                                                                                     |
| naming_series         | POS Invoice                 | ACC-PSINV-.YYYY.-                                                                                                   |
| naming_series         | Shareholder                 | ACC-SH-.YYYY.-                                                                                                      |
| naming_series         | Sales Invoice               | ACC-SINV-.YYYY.-
ACC-SINV-RET-.YYYY.-                                                                               |
| naming_series         | Closing Stock Balance       | CBAL-.#####                                                                                                         |
| naming_series         | Lead                        | CRM-LEAD-.YYYY.-                                                                                                    |
| naming_series         | Opportunity                 | CRM-OPP-.YYYY.-                                                                                                     |
| naming_series         | Customer                    | CUST-.YYYY.-                                                                                                        |
| naming_series         | Downtime Entry              | DT-                                                                                                                 |
| naming_series         | Dunning                     | DUNN-.MM.-.YY.-                                                                                                     |
| naming_series         | Driver                      | HR-DRI-.YYYY.-                                                                                                      |
| naming_series         | Employee                    | HR-EMP-                                                                                                             |
| naming_series         | Issue                       | ISS-.YYYY.-                                                                                                         |
| naming_series         | Delivery Note               | MAT-DN-.YYYY.-
MAT-DN-RET-.YYYY.-                                                                                   |
| naming_series         | Delivery Trip               | MAT-DT-.YYYY.-                                                                                                      |
| naming_series         | Installation Note           | MAT-INS-.YYYY.-                                                                                                     |
| naming_series         | Landed Cost Voucher         | MAT-LCV-.YYYY.-                                                                                                     |
| naming_series         | Material Request            | MAT-MR-.YYYY.-                                                                                                      |
| naming_series         | Maintenance Schedule        | MAT-MSH-.YYYY.-                                                                                                     |
| naming_series         | Maintenance Visit           | MAT-MVS-.YYYY.-                                                                                                     |
| naming_series         | Packing Slip                | MAT-PAC-.YYYY.-                                                                                                     |
| naming_series         | Purchase Receipt            | MAT-PRE-.YYYY.-
MAT-PR-RET-.YYYY.-                                                                                  |
| naming_series         | Quality Inspection          | MAT-QA-.YYYY.-                                                                                                      |
| naming_series         | Stock Reconciliation        | MAT-RECO-.YYYY.-                                                                                                    |
| naming_series         | Subcontracting Receipt      | MAT-SCR-.YYYY.-
MAT-SCR-RET-.YYYY.-                                                                                 |
| naming_series         | Stock Entry                 | MAT-STE-.YYYY.-                                                                                                     |
| naming_series         | Blanket Order               | MFG-BLR-.YYYY.-                                                                                                     |
| naming_series         | Production Plan             | MFG-PP-.YYYY.-                                                                                                      |
| naming_series         | Work Order                  | MFG-WO-.YYYY.-                                                                                                      |
| naming_series         | Payment Order               | PMO-                                                                                                                |
| naming_series         | Job Card                    | PO-JOB.#####                                                                                                        |
| naming_series         | Cashier Closing             | POS-CLO-                                                                                                            |
| naming_series         | Pricing Rule                | PRLE-.####                                                                                                          |
| naming_series         | Project                     | PROJ-.####                                                                                                          |
| naming_series         | Project Update              | PROJ-UPD-.YYYY.-                                                                                                    |
| naming_series         | Supplier Scorecard Period   | PU-SSP-.YYYY.-                                                                                                      |
| naming_series         | Purchase Order              | PUR-ORD-.YYYY.-                                                                                                     |
| naming_series         | Request for Quotation       | PUR-RFQ-.YYYY.-                                                                                                     |
| naming_series         | Supplier Quotation          | PUR-SQTN-.YYYY.-                                                                                                    |
| naming_series         | Serial and Batch Bundle     | SABB-.########                                                                                                      |
| naming_series         | Campaign                    | SAL-CAM-.YYYY.-                                                                                                     |
| naming_series         | Sales Order                 | SAL-ORD-.YYYY.-                                                                                                     |
| naming_series         | Quotation                   | SAL-QTN-.YYYY.-                                                                                                     |
| naming_series         | Subcontracting Order        | SC-ORD-.YYYY.-                                                                                                      |
| naming_series         | Warranty Claim              | SER-WRN-.YYYY.-                                                                                                     |
| naming_series         | Item                        | STO-ITEM-.YYYY.-                                                                                                    |
| naming_series         | Pick List                   | STO-PICK-.YYYY.-                                                                                                    |
| naming_series         | Supplier                    | SUP-.YYYY.-                                                                                                         |
| naming_series         | Timesheet                   | TS-.YYYY.-                                                                                                          |
+-----------------------+-----------------------------+---------------------------------------------------------------------------------------------------------------------+
68 rows in set (0.002 sec)
1 Like