Bench migrate error (1054, "Unknown column 'incoming_rate' in 'field list'")

after fresh installation v13 and restore site backup when try bench migrate i got below error

Reposting Stock Ledger Entries...
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 104, in <module>
    main()
  File "/home/seafood/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 19, in main
    click.Group(commands=commands)(prog_name='bench')
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 27, in _func
    ret = f(frappe._dict(ctx.obj), *args, **kwargs)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/commands/site.py", line 306, in migrate
    migrate(
  File "/home/seafood/frappe-bench/apps/frappe/frappe/migrate.py", line 67, in migrate
    frappe.modules.patch_handler.run_all(skip_failing)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 41, in run_all
    run_patch(patch)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 30, in run_patch
    if not run_single(patchmodule = patch):
  File "/home/seafood/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 71, in run_single
    return execute_patch(patchmodule, method, methodargs)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/modules/patch_handler.py", line 91, in execute_patch
    frappe.get_attr(patchmodule.split()[0] + ".execute")()
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/patches/v13_0/item_reposting_for_incorrect_sl_and_gl.py", line 44, in execute
    update_entries_after({
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 177, in __init__
    self.build()
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 245, in build
    self.process_sle_against_current_timestamp()
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 269, in process_sle_against_current_timestamp
    self.process_sle(sle)
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 371, in process_sle
    self.update_outgoing_rate_on_transaction(sle)
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 443, in update_outgoing_rate_on_transaction
    self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate)
  File "/home/seafood/frappe-bench/apps/erpnext/erpnext/stock/stock_ledger.py", line 461, in update_rate_on_delivery_and_sales_return
    frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate)
  File "/home/seafood/frappe-bench/apps/frappe/frappe/database/database.py", line 665, in set_value
    self.sql("""update `tab{0}`
  File "/home/seafood/frappe-bench/apps/frappe/frappe/database/database.py", line 146, in sql
    self._cursor.execute(query, values)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "/home/seafood/frappe-bench/env/lib/python3.8/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.OperationalError: (1054, "Unknown column 'incoming_rate' in 'field list'")

same error

i make a work around by editing this file and after finish migration make bench update again

Was the problem resolved this way??

Just a guess, and probably a fix, I face a similar problem on Opportunity,
Shortcut explanation is doctype json file have attribute “modified” date inside. Doctype into TabDocType table have also column modified date.
If they are the same during the patch update, frappe.reload_doc test it and do not perform ALTER TABLE column modify…
So two solutions
1 - Sql before launch bench upgrade --patch or switch to branch
UPDATE tabDocType SET modified=‘2020-09-08 11:10:35.318872’ WHERE name=‘Stock Ledger Entry’
2- change de docyte json file modifed attribute and commit before launch bench upgrade --patch

I’ve just submit a PR for Opportunity migration bug.

Second guess… Some developpement are back ported from v13 or develop into v12 with the same modified date into json doctype file, and this is a real bad practice regarding how upgrade process works.
If some column name change, this json doctype “modified” must be different between version…

Yeas and also you can check @FHenry solution

Installed Apps

ERPNext: v13.39.1 (HEAD)
Frappe Framework: v13.41.2 (HEAD)

I was on ERPNEXT v13.31 and just updated to the above with a migration with no errors. My instance is on Frappe Cloud. However, when I try to create a purchase order from a sales order I get the following error:

Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 69, in application
response = frappe.api.handle()
File “apps/frappe/frappe/api.py”, line 55, in handle
return frappe.handler.handle()
File “apps/frappe/frappe/handler.py”, line 38, in handle
data = execute_cmd(cmd)
File “apps/frappe/frappe/handler.py”, line 76, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “apps/frappe/frappe/init.py”, line 1457, in call
return fn(*args, **newargs)
File “apps/frappe/frappe/desk/form/save.py”, line 24, in savedocs
doc.save()
File “apps/frappe/frappe/model/document.py”, line 310, in save
return self._save(*args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 332, in _save
return self.insert()
File “apps/frappe/frappe/model/document.py”, line 261, in insert
self.run_before_save_methods()
File “apps/frappe/frappe/model/document.py”, line 1052, in run_before_save_methods
self.run_method(“validate”)
File “apps/frappe/frappe/model/document.py”, line 941, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 1261, in composer
return composed(self, method, *args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 1243, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “apps/frappe/frappe/model/document.py”, line 938, in fn
return method_object(*args, **kwargs)
File “apps/erpnext/erpnext/buying/doctype/purchase_order/purchase_order.py”, line 54, in validate
super(PurchaseOrder, self).validate()
File “apps/erpnext/erpnext/controllers/buying_controller.py”, line 33, in validate
super(BuyingController, self).validate()
File “apps/erpnext/erpnext/controllers/stock_controller.py”, line 42, in validate
super(StockController, self).validate()
File “apps/erpnext/erpnext/controllers/accounts_controller.py”, line 155, in validate
self.set_incoming_rate()
File “apps/erpnext/erpnext/controllers/buying_controller.py”, line 307, in set_incoming_rate
frappe.db.get_value(ref_doctype, d.get(frappe.scrub(ref_doctype)), field)
File “apps/frappe/frappe/database/database.py”, line 469, in get_value
limit=1,
File “apps/frappe/frappe/database/database.py”, line 540, in get_values
limit=limit,
File “apps/frappe/frappe/database/database.py”, line 739, in _get_values_from_table
update=update,
File “apps/frappe/frappe/database/database.py”, line 174, in sql
self._cursor.execute(query, values)
File “env/lib/python3.7/site-packages/pymysql/cursors.py”, line 148, in execute
result = self._query(query)
File “env/lib/python3.7/site-packages/pymysql/cursors.py”, line 310, in _query
conn.query(q)
File “env/lib/python3.7/site-packages/pymysql/connections.py”, line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “env/lib/python3.7/site-packages/pymysql/connections.py”, line 775, in _read_query_result
result.read()
File “env/lib/python3.7/site-packages/pymysql/connections.py”, line 1156, in read
first_packet = self.connection._read_packet()
File “env/lib/python3.7/site-packages/pymysql/connections.py”, line 725, in _read_packet
packet.raise_for_error()
File “env/lib/python3.7/site-packages/pymysql/protocol.py”, line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File “env/lib/python3.7/site-packages/pymysql/err.py”, line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1054, “Unknown column ‘incoming_rate’ in ‘field list’”)

I am not able to save the purchase order.

I guess this is bug that is persisting in other parts of the code.

Any ideas how it can be resolved?