[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.
This commit is contained in:
Raphael Collet 2015-09-08 15:09:35 +02:00
parent b89b7e0554
commit 085875619f
1 changed files with 3 additions and 6 deletions

View File

@ -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: