Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions odoo/addons/base/wizard/base_partner_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,23 @@ def update_records(model, src, field_model='model', field_id='res_id'):
with mute_logger('odoo.sql_db'), self._cr.savepoint():
records.sudo().write({field_id: dst_record.id})
records.env.flush_all()
except psycopg2.Error:
# updating fails, most likely due to a violated unique constraint
# keeping record with nonexistent partner_id is useless, better delete it
records.sudo().unlink()
except psycopg2.Error as e:
# updating fails, if the model of records is in allowed ones to delete in case
# of unique violation error then delete it, in case is not in allowed ones or
# postgresql error is not unique violation, then raise an error
model_names_to_delete = self.env['ir.config_parameter'].sudo().get_param("base_partner_merge.models_allowed_to_unlink")
model_names_to_delete = model_names_to_delete.split(",") if model_names_to_delete else []
if not model_names_to_delete or (records._name in model_names_to_delete and e.pgcode == psycopg2.errorcodes.UNIQUE_VIOLATION):
records.sudo().unlink()
elif model_names_to_delete:
# Everything else is not expected and should fail.
msg = """An error has ocurred meanwhile reference fields were updated: \nDestination Record: %s \nSource Record: %s, \nError: %s""" % (
dst_record.id,
tuple(src_records.ids),
e,
)
_logger.error(msg)
raise UserError(msg)

for record in src_records:
update_records('ir.attachment', src=record, field_model='res_model')
Expand Down