# Buggy code
def get_gl_entries(filters):
    currency_map = get_currency(filters)
    select_fields = """, debit, credit, debit_in_account_currency,
		credit_in_account_currency """

    order_by_statement = "order by posting_date, account, creation"

    if filters.get("group_by") == _("Group by Voucher"):
        order_by_statement = "order by posting_date, voucher_type, voucher_no"

    if filters.get("include_default_book_entries"):
        filters["company_fb"] = frappe.db.get_value(
            "Company", filters.get("company"), "default_finance_book"
        )

    distributed_cost_center_query = ""
    if filters and filters.get("cost_center"):
        select_fields_with_percentage = """, debit*(DCC_allocation.percentage_allocation/100) as debit, credit*(DCC_allocation.percentage_allocation/100) as credit, debit_in_account_currency*(DCC_allocation.percentage_allocation/100) as debit_in_account_currency,
		credit_in_account_currency*(DCC_allocation.percentage_allocation/100) as credit_in_account_currency """

        distributed_cost_center_query = """
		UNION ALL
		SELECT name as gl_entry,
			posting_date,
			account,
			party_type,
			party,
			voucher_type,
			voucher_no,
			cost_center, project,
			against_voucher_type,
			against_voucher,
			account_currency,
			remarks, against, 
			is_opening, `tabGL Entry`.creation {select_fields_with_percentage}
		FROM `tabGL Entry`,
		(
			SELECT parent, sum(percentage_allocation) as percentage_allocation
			FROM `tabDistributed Cost Center`
			WHERE cost_center IN %(cost_center)s
			AND parent NOT IN %(cost_center)s
			GROUP BY parent
		) as DCC_allocation
		WHERE company=%(company)s
		{conditions}
		AND posting_date <= %(to_date)s
		AND cost_center = DCC_allocation.parent
		""".format(
            select_fields_with_percentage=select_fields_with_percentage,
            conditions=get_conditions(filters).replace(
                "and cost_center in %(cost_center)s ", ""
            ),
        )

    gl_entries = frappe.db.sql(
        """
		select
			name as gl_entry, posting_date, account, party_type, party,
			voucher_type, voucher_no, cost_center, project,
			against_voucher_type, against_voucher, account_currency,
			remarks, against, is_opening, creation {select_fields}
		from `tabGL Entry`
		where company=%(company)s {conditions}
		{distributed_cost_center_query}
		{order_by_statement}
		""".format(
            select_fields=select_fields,
            conditions=get_conditions(filters),
            distributed_cost_center_query=distributed_cost_center_query,
            order_by_statement=order_by_statement,
        ),
        filters,
        as_dict=1,
    )

    if filters.get("presentation_currency"):
        return convert_to_presentation_currency(gl_entries, currency_map)
    else:
        return gl_entries


# 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 get_gl_entries(filters):
    currency_map = get_currency(filters)
    select_fields = """, debit, credit, debit_in_account_currency,
		credit_in_account_currency """

    order_by_statement = "order by posting_date, account, creation"

    if filters.get("group_by") == _("Group by Voucher"):
        order_by_statement = "order by posting_date, voucher_type, voucher_no"

    if filters.get("include_default_book_entries"):
        filters["company_fb"] = frappe.db.get_value(
            "Company", filters.get("company"), "default_finance_book"
        )

    distributed_cost_center_query = ""
    if filters and filters.get("cost_center"):
        select_fields_with_percentage = """, debit*(DCC_allocation.percentage_allocation/100) as debit, credit*(DCC_allocation.percentage_allocation/100) as credit, debit_in_account_currency*(DCC_allocation.percentage_allocation/100) as debit_in_account_currency,
		credit_in_account_currency*(DCC_allocation.percentage_allocation/100) as credit_in_account_currency """

        distributed_cost_center_query = """
		UNION ALL
		SELECT name as gl_entry,
			posting_date,
			account,
			party_type,
			party,
			voucher_type,
			voucher_no,
			cost_center, project,
			against_voucher_type,
			against_voucher,
			account_currency,
			remarks, against, 
			is_opening, `tabGL Entry`.creation {select_fields_with_percentage}
		FROM `tabGL Entry`,
		(
			SELECT parent, sum(percentage_allocation) as percentage_allocation
			FROM `tabDistributed Cost Center`
			WHERE cost_center IN %(cost_center)s
			AND parent NOT IN %(cost_center)s
			GROUP BY parent
		) as DCC_allocation
		WHERE company=%(company)s
		{conditions}
		AND posting_date <= %(to_date)s
		AND cost_center = DCC_allocation.parent
		""".format(
            select_fields_with_percentage=select_fields_with_percentage,
            conditions=get_conditions(filters).replace(
                "and cost_center in %(cost_center)s ", ""
            ),
        )

    gl_entries = frappe.db.sql(
        """
		select
			name as gl_entry, posting_date, account, party_type, party,
			voucher_type, voucher_no, cost_center, project,
			against_voucher_type, against_voucher, account_currency,
			remarks, against, is_opening, creation {select_fields}
		from `tabGL Entry`
		where company=%(company)s {conditions}
		{distributed_cost_center_query}
		{order_by_statement}
		""".format(
            select_fields=select_fields,
            conditions=get_conditions(filters),
            distributed_cost_center_query=distributed_cost_center_query,
            order_by_statement=order_by_statement,
        ),
        filters,
        as_dict=1,
    )

    if filters.get("presentation_currency"):
        return convert_to_presentation_currency(
            gl_entries, currency_map, filters.get("company")
        )
    else:
        return gl_entries