Changing an Odoo Field from Selection to Char with a Pre-Migrate Script
Changing an Odoo Field from Selection to Char with a Pre-Migrate Script
At some point a selection field has to become a char. For example, a fixed duration choice (15, 30, 60, 90) needs to become free text such as 1h00 or 1h30. It sounds like you only need to change the field definition and upgrade the module. In practice, Odoo refuses.
The problem: the ORM does not change field types
During a module upgrade, the ORM compares the field definition with the metadata already stored in ir.model.fields. A change of type (ttype) on an existing field is not applied automatically. The upgrade can fail, or old data no longer matches the new type.
There is an extra wrinkle: the old values in the database are selection keys ('15', '60'), while the new display wants labels ('15 min', '1h00'). Without a mapping, old rows show up as raw numbers.
The solution: do it in pre-migrate
A pre-migrate script runs before the module is updated and before XML data is loaded. That is the right place to prepare the schema so the ORM finds a state that already matches the new definition.
Layout:
migrations/
0.2.43/
pre-migrate.py
The basic content (simplified):
TIME_ON_SITE_LABELS = { "15": "15 min", "30": "30 min", "45": "45 min", "60": "1h00", "90": "1h30", "120": "2h00", } def _column_exists(cr, table, column): cr.execute( """ SELECT 1 FROM information_schema.columns WHERE table_name = %s AND column_name = %s """, (table, column), ) return bool(cr.fetchone()) def _map_time_on_site(cr, table, column): if not _column_exists(cr, table, column): return for old, new in TIME_ON_SITE_LABELS.items(): cr.execute( f'UPDATE "{table}" SET "{column}" = %s WHERE "{column}" = %s', (new, old), ) def migrate(cr, version): ...
The important steps:
- Map old values to new labels with SQL, before the XML data loads.
- Align the field metadata (
ir.model.fields) so its type matches the new definition and the ORM stops refusing. - Let the XML/data load the new definition afterwards, because the database state already matches.
A must: make the script idempotent
A migration can fail halfway and be run again, or run on a database that never had the field. The script must be safe to repeat:
- Check whether the column or table exists first (
information_schema) and skip if it does not. - Filter by state: only touch rows that still hold old values, or only change metadata that is still
selection. - Never assume the data is always there.
That way, running it twice breaks nothing.
Be careful with f-strings in SQL
In the snippet above, table and column names are inserted with an f-string, while values still use parameters (%s). That is only safe when the table and column names are internal constants, not user input. Values must never be built with an f-string. For dynamic identifiers, consider a built-in helper such as psycopg2.sql.Identifier (or odoo.tools.SQL), so it stays safe if the source changes someday.
What cannot be undone
Direct SQL on tables and metadata has no automatic rollback the way the ORM does. So:
- Back up first, before running the upgrade.
- Test on a copy of the database with real data, not demo data.
- Compare row counts before and after, and spot-check a few mapped values.
- Have a plan for values the mapping misses (for example an old key that is not in the dictionary).
Quick checklist
- A field type change needs a migration script, not just an edited definition.
- Use
pre-migrateto prepare the schema before the ORM loads the new definition. - Map old values to new values explicitly.
- Make it idempotent: check columns, filter by state, never assume data.
- Parameterize values, and make sure identifiers come from constants.
- Back up, test on a copy, and verify the result with counts.
Closing
Data migration often feels like a small chore at the end, yet it is where customer data is most at risk. A small, explicit, idempotent script is far easier to trust than a clever one that cannot be repeated.
Author: Aditya Oryza Mahendra
Software Engineer with expertise in backend systems, Odoo ERP development (v11–19), and Web3/dApp development. Experienced in building scalable applications using Node.js, Express.js, React, and Next.js, alongside deep customization of Odoo ERP across logistics, finance, CRM, and manufacturing domains. Currently advancing into Web3 with hands-on experience building and deploying smart contracts and decentralized applications using Solidity, Hardhat, and Ethers.js. Strong background in SQL/NoSQL databases, Docker, and VPS/Linux server administration. Contributed to the automation of 500+ daily shipments at a national logistics company.
Connect on LinkedInRelated Articles
Keep an Odoo Computed Field Value When the Customer Changes
A stored computed Many2one with readonly=False resets whenever its dependency changes. A keep-if-valid rule fixes it across CRM, sales and invoices.
Copy an Odoo Production Database Safely: Why a Plain Restore Is Not Enough
Restoring a production database also restores its live e-mail, cron and storage settings. Here is how to copy it to your laptop safely, in three layers.