Debugging Odoo effectively often means working both at the database level (PostgreSQL) and the application level (Odoo shell). By combining psql and Odoo’s Python REPL (shell), you’ll gain deep insight into how Odoo stores data, how its ORM works, and how your custom code interacts with the underlying schema. In this article, we’ll explore how to use these tools to diagnose issues, validate assumptions, and fix bugs more reliably.
Why Use Psql and Shell for Debugging
- psql (PostgreSQL-level): You can directly query tables, inspect data, check relationships, find orphan records, or validate constraints. This is useful when you suspect data corruption, or want to understand how Odoo has persisted data.
- Odoo Shell (REPL): This gives you access to Odoo’s env (environment), models, and ORM. You can test methods, run business logic, simulate operations, and commit or rollback transactions. It’s faster to iterate here than writing temporary scripts or running UI flows.
- Combined: Use psql to confirm or fix data inconsistencies, and then validate with the shell to see how your changes affect the ORM behavior.
Preparing Environment
Before jumping into debugging, make sure your environment is set up correctly:
Access to PostgreSQL
- Make sure you can connect to your Odoo database via psql.
- If PostgreSQL is running locally:
psql -u <db_user> -d <your_odoo_db_name>
You can start the Odoo Shell with:
./odoo-bin shell -d <your_db> -c /path/to/odoo.conf
Debugging with PostgreSQL
Here are strategies for using psql to debug Odoo:
Inspect Data and Relationships
- List tables: Use \dt to view all tables in your Odoo database.
- Check specific tables: For example, to inspect partners:
SELECT id, name, FROM res_partner LIMIT 50;
- Join tables: To understand relations, e.g., sale_order and res_partner:
SELECT so.id, so.partner_id, rp.name
FROM sale_order so
JOIN res_partner rp ON so.partner_id = rp.id
LIMIT 100;
Find Orphan or Invalid Records
if a record references a partner that no longer exists, you can detect it:
SELECT so.id FROM sale_order so LEFT JOIN res_partner rp ON so.partner_id = rp.id WHERE rp.id IS NULL;
Modify Data (with Caution)
You can update or delete records directly in psql, but this bypasses Odoo's ORM logic (business rules, computed fields, constraints).
Example update:
UPDATE res_partner SET name = 'Corrected Name' WHERE id = 42;
After raw changes, you must be very careful: the Odoo cache may be inconsistent, and ORM may not reflect your changes properly. According to Odoo best practices, you might need to invalidate the cache in the environment if you manipulate data directly.
Debugging with Odoo Shell (ORM Level)
Using the Odoo shell gives you interactive access to the models, records, and business logic.
When you launch the shell via the CLI:
./odoo-bin shell -d your_db -c /path/to/odoo.conf
Now you have env loaded:
In [1]: env Out[1]: <Environment object>
Exploring Models
- You can query records via ORM:
partners = env['res.partner'].search([]) print(partners)
- Inspect a specific record:
partner = env['res.partner'].browse(42) partner.name
Testing Methods & Business Logic
- Call model methods:
total = env['sale.order'].browse(10).amount_total print(total)
- Create a temporary record:
new_partner = env['res.partner'].create({'name': 'Test Debug'})
print(new_partner.id)
env.cr.commit() # commit if you want to persist
Best Practices & Risks
- Use psql only when necessary: Direct SQL changes bypass the ORM, so you risk breaking data invariants or losing business logic.
- Invalidate cache when needed: After raw SQL changes, Odoo’s in-memory cache may not reflect the new state. Use shell to reload or re-initialize environment if needed.
- Use version control: Changes in Python code (with breakpoints) should be under version control. Don’t leave breakpoints in production code.
- Backup always: Before any destructive operations, backup your database.
- Limit debugging in production: Use debug tools carefully in production environments. For heavy debugging, replicate production data in a staging area.
Conclusion
Debugging Odoo using both psql and the Odoo shell is one of the most powerful combinations a developer can use. psql offers raw visibility into the database, helping you find data-level problems. The Odoo shell gives you context, letting you run ORM calls, simulate logic, and test your fixes interactively. If you do it carefully—with backups, logging, and proper transaction control—you’ll dramatically reduce the time it takes to find and fix issues.
