pymysql.err.OperationalError: (1054, “Unknown column ‘fee_schedule’ in ‘where clause’”)

Hello,

I was tying to add a new fee structure but when I click on save it shows this error:

pymysql.err.OperationalError: (1054, “Unknown column ‘fee_schedule’ in ‘where clause’”)
Possible source of error: education (app)

App Versions

{
	"education": "15.2.1",
	"erpnext": "15.17.1",
	"frappe": "15.17.3",
	"healthcare": "16.0.0-dev",
	"hrms": "15.14.0",
	"payments": "0.0.1"
}

Any help is appreciated.

Traceback

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 110, in application
    response = frappe.api.handle(request)
  File "apps/frappe/frappe/api/__init__.py", line 49, in handle
    data = endpoint(**arguments)
  File "apps/frappe/frappe/api/v1.py", line 36, in handle_rpc_call
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 49, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 85, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1716, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/utils/typing_validations.py", line 31, in wrapper
    return func(*args, **kwargs)
  File "apps/frappe/frappe/__init__.py", line 876, in wrapper_fn
    retval = fn(*args, **get_newargs(fn, kwargs))
  File "apps/frappe/frappe/desk/notifications.py", line 289, in get_open_count
    external_links_data_for_d = get_external_links(d, name, links)
  File "apps/frappe/frappe/desk/notifications.py", line 338, in get_external_links
    frappe.get_all(
  File "apps/frappe/frappe/__init__.py", line 2005, in get_all
    return get_list(doctype, *args, **kwargs)
  File "apps/frappe/frappe/__init__.py", line 1980, in get_list
    return frappe.model.db_query.DatabaseQuery(doctype).execute(*args, **kwargs)
  File "apps/frappe/frappe/model/db_query.py", line 192, in execute
    result = self.build_and_run()
  File "apps/frappe/frappe/model/db_query.py", line 233, in build_and_run
    return frappe.db.sql(
  File "apps/frappe/frappe/database/database.py", line 234, in sql
    self._cursor.execute(query, values)
  File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 153, in execute
    result = self._query(query)
  File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 558, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 822, in _read_query_result
    result.read()
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 1200, in read
    first_packet = self.connection._read_packet()
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 772, in _read_packet
    packet.raise_for_error()
  File "env/lib/python3.10/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "env/lib/python3.10/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.OperationalError: (1054, "Unknown column 'tabSales Invoice.student' in 'where clause'")

Request Data

{
	"type": "GET",
	"args": {
		"doctype": "Student",
		"name": "EDU-STU-2024-00004",
		"items": "[\"Sales Invoice\",\"Bank Account\",\"Program Enrollment\",\"Course Enrollment\",\"Student Log\",\"Student Group\",\"Assessment Result\",\"Student Attendance\",\"Student Leave Application\"]"
	},
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.notifications.get_open_count",
	"request_id": null
}

Response Data

{
	"exception": "pymysql.err.OperationalError: (1054, \"Unknown column 'tabSales Invoice.student' in 'where clause'\")",
	"exc_type": "OperationalError"
}

Anyone please help!

The error you’re encountering indicates that the column fee_schedule (or student in the secondary error) does not exist in the specified table (tabSales Invoice in this case) within the database. This usually happens due to database schema changes that were not applied correctly or the code trying to access a column that was not defined in the database schema.

Here are steps to diagnose and fix the issue:

  1. Verify Database Schema:
    Ensure that the fee_schedule and student columns exist in the relevant tables in your database. You can use a database client (like MySQL Workbench) to check the schema.

    SHOW COLUMNS FROM `tabSales Invoice`;
    
  2. Run Migrations:
    Running migrations ensures that all necessary database schema changes are applied. Execute the following commands:

    bench migrate
    
  3. Check Customizations:
    If you have made any customizations or installed custom apps, ensure they are not causing conflicts by trying to access non-existent columns.

  4. Check Doctype Definitions:
    Make sure the fee_schedule and student fields are defined in the appropriate DocType JSON files. These files are usually located in the app directory under doctype.

  5. Clear Cache:
    Clear the cache to ensure there are no old references to columns or fields that might be causing the issue:

    bench clear-cache
    bench clear-website-cache
    bench restart
    
  6. Review Recent Changes:
    If the error started occurring after a recent update or installation, review the changes made to the codebase and ensure that all migrations were applied correctly.

  7. Update the Apps:
    Ensure that all apps are up to date. Sometimes, bugs are fixed in newer releases.

    bench update --reset
    
  8. Check for Errors in Custom Scripts:
    Ensure there are no custom scripts or server-side scripts that reference non-existent columns.

Detailed Steps

Step 1: Verify Database Schema
Check if the columns exist in the tabSales Invoice table.

DESCRIBE `tabSales Invoice`;

Step 2: Run Migrations

bench --site yoursite.name migrate

Step 3: Clear Cache and Restart

bench clear-cache
bench clear-website-cache
bench restart

Step 4: Check Doctype Definitions
Verify the definitions in your app’s doctype folder. For example, check:

apps/education/education/doctype/fee_structure/fee_structure.json