Odoo - Many2one implementation in database

1.2k views Asked by At

While debugging a module I have the need to inspect a many2one field directly in postgres.

How can I find the account_move rows in database that belong to a given hr_expense_sheet row? Is ir_model_data table used to implement the many2one relation?

class HrExpenseSheet(models.Model):
....
    account_move_id = fields.Many2one('account.move', string='Journal Entry', copy=False)
1

There are 1 answers

1
travisw On BEST ANSWER

You don't really provide quite enough information for me to know the exact relationships between each of the models, but here's a general idea.

Is ir_model_data table used to implement the many2one relation?

The Many2one relation is managed with a foreign key. So, on the HrExpenseSheet model, it stores the account_move_id as an ID. If you want the AccountMove information directly from the database, you must search the account_move for that given ID.

Assuming the models a related something like this:

class AccountMove(models.Model):
    _inherit = "account.move"
    ...

class HrExpense(models.Model):
    _inherit = "hr.expense"
    ...

class HrExpenseSheet(models.Model):
    _inherit = "hr.expense.sheet"

    account_move_id = fields.Many2one('account.move', string='Journal Entry', copy=False)
    expense_id = fields.Many2one('hr.expense', string='Expense', copy=False)

Basically, the query you will run is going to find all account moves for any line that belongs to a given expense.

SELECT * FROM account_move WHERE id IN (
    SELECT account_move_id FROM hr_expense_sheet WHERE expense_id = 123);

If you just want to find for a specific HrExpenseSheet, you can use:

SELECT * FROM account_move WHERE id = 123;