From 83bf159aef9198cbfefee5a38fe19d4120fe8e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 23:28:40 +0100 Subject: [PATCH] Check foreign key in last on database integrity error The search is done on a new transaction so it can not find targets that were created during the main transaction. --- trytond/model/modelsql.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 4d2ddced30..abb68d1bf6 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -352,6 +352,15 @@ class ModelSQL(ModelStorage): raise RequiredValidationError( gettext('ir.msg_required_validation_record', **cls._get_error_args(field_name))) + for name, _, error in cls._sql_constraints: + if TableHandler.convert_name(name) in str(exception): + raise SQLConstraintError(gettext(error)) + # Check foreign key in last because this can raise false positive + # if the target is created during the same transaction. + for field_name in field_names: + if field_name not in cls._fields: + continue + field = cls._fields[field_name] if isinstance(field, fields.Many2One) and values.get(field_name): Model = pool.get(field.model_name) create_records = transaction.create_records.get( @@ -369,9 +378,6 @@ class ModelSQL(ModelStorage): raise ForeignKeyError( gettext('ir.msg_foreign_model_missing', **error_args)) - for name, _, error in cls._sql_constraints: - if TableHandler.convert_name(name) in str(exception): - raise SQLConstraintError(gettext(error)) @classmethod def history_revisions(cls, ids):