From 085875619fbbebfcb1a4e6bb4ff28d109bd4b07b Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Sep 2015 15:09:35 +0200 Subject: [PATCH] [FIX] fields: in `one2many.set`, replace incorrect query by ORM access When linking a record into a `one2many` relation with command `(4, rid)`, a query checks whether the record is already linked to the current record id: SELECT 1 FROM {inv_table} WHERE id={rid} AND {inv_field}={id} where `inv_field` is the name of the inverse field, and `inv_table` is the table where this field is stored. The query is wrong if the inverse field is inherited, because the `rid` does not belong to the table `inv_table`. The test has been replaced by a plain ORM access: rec = obj.browse(cr, SUPERUSER_ID, rid) if int(rec[inv_field]) != id: ... This fixes #4685. --- openerp/osv/fields.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index 3b7a18edf53..76aecfe2b84 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -823,12 +823,9 @@ class one2many(_column): else: cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],)) elif act[0] == 4: - # table of the field (parent_model in case of inherit) - field = obj.pool[self._obj]._fields[self._fields_id] - field_model = field.base_field.model_name - field_table = obj.pool[field_model]._table - cr.execute("select 1 from {0} where id=%s and {1}=%s".format(field_table, self._fields_id), (act[1], id)) - if not cr.fetchone(): + # check whether the given record is already linked + rec = obj.browse(cr, SUPERUSER_ID, act[1], {'prefetch_fields': False}) + if int(rec[self._fields_id]) != id: # Must use write() to recompute parent_store structure if needed and check access rules obj.write(cr, user, [act[1]], {self._fields_id:id}, context=context or {}) elif act[0] == 5: