Custom Fields from Patches Not Reflecting in Core Doctypes (Company & Employee)

@NCP @tousiff @avc

I’m currently working on a large-scale Frappe app that includes around 400+ patches, most of which add or update Custom Fields in core doctypes such as Company and Employee.

All patches execute successfully — the new custom fields appear in the Custom Field list, and the tabCustom Field table in the database shows correct entries.
However, many of these fields do not reflect:

  • In the actual Doctype Form View, or

  • In the Customize Form view (some fields appear, some do not).

In certain cases:

  • A field appears in Customize Form but not on the actual form.

  • Some fields are completely missing in both, even though the DB shows them correctly.


What I’ve Already Tried

  1. :white_check_mark: Verified that all patches are listed in show-patches and executed successfully.

  2. :white_check_mark: Confirmed that tabCustom Field entries exist for each field with the correct dt, fieldname, and insert_after.

  3. :white_check_mark: Cleared caches and reloaded doctypes multiple times:

    bench --site <site> clear-cache
    bench --site <site> clear-website-cache
    bench --site <site> migrate
    bench --site <site> reload-doc erpnext accounts company
    bench restart
    
    
  4. :white_check_mark: Tried resetting Customize Form and reloading metadata.

  5. :white_check_mark: Checked app load order and confirmed my custom app runs after erpnext.

  6. :white_check_mark: Added cache clear lines in patches (frappe.clear_cache(doctype="Company")).

  7. :white_check_mark: Manually reloading via console reflects the fields temporarily, but they disappear after a new build or migrate.

  8. :white_check_mark: Cleared from custom field list in Site


Observation

This seems to happen only with core doctypes (Company, Employee, etc.), not with custom doctypes created within my own app.
Custom fields added manually through the UI (Customize Form) appear correctly, but programmatically created ones (via patches) are not consistently merged into the DocType metadata cache.

In some instances, the fields load after a restart, but then vanish again once another migration or build runs — likely due to core JSON reload overriding the runtime metadata.


Questions

  1. Is this a known issue where core doctypes ignore patch-based Custom Fields due to Frappe’s internal metadata caching or JSON reloading behavior?

  2. What is the best long-term, update-safe approach when you have hundreds of programmatically added fields for core doctypes?

    • Or is there a way to make patches “stick” permanently to core doctypes?
  3. Is there a reliable command or API to force metadata rebuild for core doctypes so the fields merge properly after patch execution?


What I’m Looking For

  • A clear, Frappe-supported best practice for maintaining large-scale patch-based customizations on core doctypes.

  • Confirmation on whether fixtures are the only safe, persistent solution in this case.

  • Any internal mechanism or workaround (like an event hook or metadata rebuild command) to ensure custom fields persist reliably on core doctypes after every build/migrate.


Summary

Despite successful patch execution and visible entries in the Custom Field list, many programmatically added fields in core doctypes are not appearing in the actual form or customize form, suggesting a desync between database-level custom fields and Frappe’s metadata cache or JSON reload mechanism.

Would appreciate any guidance or official clarification on the proper, update-safe, and scalable method to manage hundreds of custom fields across core doctypes.

Hello,

Have you tried the following steps?

  1. Go to Patch Log doctype.

  2. Find the log entry with the Patch ID corresponding to your custom patch (the one that creates those custom fields).

  3. Delete that Patch Log entry.

    • If you don’t have permission to delete it, go to Role Permissions Manager and temporarily add Delete permission for the Patch Log doctype to a role you have.
  4. Once deleted, run the following command:

    bench --site <your-site-name> migrate
    
    
  5. Log in again and check if the custom fields now appear as expected.

Already did but still not working