# Buggy code
def set_gl_entries_by_account(
    from_date,
    to_date,
    root_lft,
    root_rgt,
    filters,
    gl_entries_by_account,
    accounts_by_name,
    ignore_closing_entries=False,
):
    """Returns a dict like { "account": [gl entries], ... }"""

    company_lft, company_rgt = frappe.get_cached_value(
        "Company", filters.get("company"), ["lft", "rgt"]
    )

    additional_conditions = get_additional_conditions(
        from_date, ignore_closing_entries, filters
    )
    companies = frappe.db.sql(
        """ select name, default_currency from `tabCompany`
		where lft >= %(company_lft)s and rgt <= %(company_rgt)s""",
        {
            "company_lft": company_lft,
            "company_rgt": company_rgt,
        },
        as_dict=1,
    )

    currency_info = frappe._dict(
        {
            "report_date": to_date,
            "presentation_currency": filters.get("presentation_currency"),
        }
    )

    for d in companies:
        gl_entries = frappe.db.sql(
            """select gl.posting_date, gl.account, gl.debit, gl.credit, gl.is_opening, gl.company,
			gl.fiscal_year, gl.debit_in_account_currency, gl.credit_in_account_currency, gl.account_currency,
			acc.account_name, acc.account_number
			from `tabGL Entry` gl, `tabAccount` acc where acc.name = gl.account and gl.company = %(company)s
			{additional_conditions} and gl.posting_date <= %(to_date)s and acc.lft >= %(lft)s and acc.rgt <= %(rgt)s
			order by gl.account, gl.posting_date""".format(
                additional_conditions=additional_conditions
            ),
            {
                "from_date": from_date,
                "to_date": to_date,
                "lft": root_lft,
                "rgt": root_rgt,
                "company": d.name,
                "finance_book": filters.get("finance_book"),
                "company_fb": frappe.db.get_value(
                    "Company", d.name, "default_finance_book"
                ),
            },
            as_dict=True,
        )

        if filters and filters.get("presentation_currency") != d.default_currency:
            currency_info["company"] = d.name
            currency_info["company_currency"] = d.default_currency
            convert_to_presentation_currency(gl_entries, currency_info)

        for entry in gl_entries:
            key = entry.account_number or entry.account_name
            validate_entries(key, entry, accounts_by_name)
            gl_entries_by_account.setdefault(key, []).append(entry)

    return gl_entries_by_account


# Traceback (pymysql.err.ProgrammingError)
$ bench --site test.site install-app erpnext

Installing erpnext...
Updating DocTypes for erpnext       : [===                                     ] 9%An error occurred while installing erpnext:
Traceback (most recent call last):
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/commands/site.py", line 215, in install_app
_install_app(app, verbose=context.verbose)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/installer.py", line 84, in install_app
sync_for(name, force=True, sync_everything=True, verbose=verbose, reset_permissions=True)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/sync.py", line 69, in sync_for
import_file_by_path(doc_path, force=force, ignore_version=True,
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 69, in import_file_by_path
import_doc(doc, force=force, data_import=data_import, pre_process=pre_process,
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/modules/import_file.py", line 143, in import_doc
doc.insert()
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 268, in insert
self.run_post_save_methods()
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 964, in run_post_save_methods
self.run_method("on_update")
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 831, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 1116, in composer
return composed(self, method, *args, **kwargs)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 1099, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/document.py", line 825, in <lambda>
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 291, in on_update
self.run_module_method("on_doctype_update")
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/core/doctype/doctype/doctype.py", line 357, in run_module_method
module = load_doctype_module(self.name, self.module)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/modules/utils.py", line 204, in load_doctype_module
doctype_python_modules[key] = frappe.get_module(module_name)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/__init__.py", line 858, in get_module
return importlib.import_module(modulename)
File "/home/haris/myproject/frappe-bench/env/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/haris/myproject/frappe-bench/apps/erpnext/erpnext/accounts/doctype/process_statement_of_accounts/process_statement_of_accounts.py", line 8, in <module>
from erpnext.accounts.report.general_ledger.general_ledger import execute as get_soa
File "/home/haris/myproject/frappe-bench/apps/erpnext/erpnext/accounts/report/general_ledger/general_ledger.py", line 7, in <module>
from erpnext.accounts.report.utils import get_currency, convert_to_presentation_currency
File "/home/haris/myproject/frappe-bench/apps/erpnext/erpnext/accounts/report/utils.py", line 10, in <module>
sum(frappe.get_list('Account', fields=['name'], or_filters=[{'root_type': 'Income'}, {'root_type': 'Expense'}], as_list=True), ())
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/__init__.py", line 1329, in get_list
return frappe.model.db_query.DatabaseQuery(doctype).execute(None, *args, **kwargs)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/db_query.py", line 96, in execute
result = self.build_and_run()
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/db_query.py", line 110, in build_and_run
args = self.prepare_args()
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/db_query.py", line 136, in prepare_args
self.set_optional_columns()
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/db_query.py", line 302, in set_optional_columns
columns = get_table_columns(self.doctype)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/model/meta.py", line 49, in get_table_columns
return frappe.db.get_table_columns(doctype)
File "/home/haris/myproject/frappe-bench/apps/frappe/frappe/database/database.py", line 893, in get_table_columns
raise self.TableMissingError('DocType', doctype)
pymysql.err.ProgrammingError: ('DocType', 'Account')

# Fixed code
def set_gl_entries_by_account(
    from_date,
    to_date,
    root_lft,
    root_rgt,
    filters,
    gl_entries_by_account,
    accounts_by_name,
    ignore_closing_entries=False,
):
    """Returns a dict like { "account": [gl entries], ... }"""

    company_lft, company_rgt = frappe.get_cached_value(
        "Company", filters.get("company"), ["lft", "rgt"]
    )

    additional_conditions = get_additional_conditions(
        from_date, ignore_closing_entries, filters
    )
    companies = frappe.db.sql(
        """ select name, default_currency from `tabCompany`
		where lft >= %(company_lft)s and rgt <= %(company_rgt)s""",
        {
            "company_lft": company_lft,
            "company_rgt": company_rgt,
        },
        as_dict=1,
    )

    currency_info = frappe._dict(
        {
            "report_date": to_date,
            "presentation_currency": filters.get("presentation_currency"),
        }
    )

    for d in companies:
        gl_entries = frappe.db.sql(
            """select gl.posting_date, gl.account, gl.debit, gl.credit, gl.is_opening, gl.company,
			gl.fiscal_year, gl.debit_in_account_currency, gl.credit_in_account_currency, gl.account_currency,
			acc.account_name, acc.account_number
			from `tabGL Entry` gl, `tabAccount` acc where acc.name = gl.account and gl.company = %(company)s
			{additional_conditions} and gl.posting_date <= %(to_date)s and acc.lft >= %(lft)s and acc.rgt <= %(rgt)s
			order by gl.account, gl.posting_date""".format(
                additional_conditions=additional_conditions
            ),
            {
                "from_date": from_date,
                "to_date": to_date,
                "lft": root_lft,
                "rgt": root_rgt,
                "company": d.name,
                "finance_book": filters.get("finance_book"),
                "company_fb": frappe.db.get_value(
                    "Company", d.name, "default_finance_book"
                ),
            },
            as_dict=True,
        )

        if filters and filters.get("presentation_currency") != d.default_currency:
            currency_info["company"] = d.name
            currency_info["company_currency"] = d.default_currency
            convert_to_presentation_currency(
                gl_entries, currency_info, filters.get("company")
            )

        for entry in gl_entries:
            key = entry.account_number or entry.account_name
            validate_entries(key, entry, accounts_by_name)
            gl_entries_by_account.setdefault(key, []).append(entry)

    return gl_entries_by_account