[FIX] crm: avoid constraint errors when merging partners

As for the _update_foreign_keys, the _update_reference_fields method may raise an unique constraint when merging two partners.
In such case, the new record is not relevant and can be removed.

Backport of 8d23a3a86c.
OPW 657338, closes #9705.
This commit is contained in:
Martin Trigaux 2014-09-02 15:08:07 +02:00 committed by David Monjoie
parent d117d6645e
commit 1660daf2fb
1 changed files with 7 additions and 1 deletions

View File

@ -220,7 +220,13 @@ class MergePartnerAutomatic(osv.TransientModel):
return
domain = [(field_model, '=', 'res.partner'), (field_id, '=', src.id)]
ids = proxy.search(cr, openerp.SUPERUSER_ID, domain, context=context)
return proxy.write(cr, openerp.SUPERUSER_ID, ids, {field_id: dst_partner.id}, context=context)
try:
with mute_logger('openerp.sql_db'), cr.savepoint():
return proxy.write(cr, openerp.SUPERUSER_ID, ids, {field_id: dst_partner.id}, context=context)
except psycopg2.Error:
# updating fails, most likely due to a violated unique constraint
# keeping record with nonexistent partner_id is useless, better delete it
return proxy.unlink(cr, openerp.SUPERUSER_ID, ids, context=context)
update_records = functools.partial(update_records, context=context)