From c5fbdcf1c63ce4ea667e7f28c9d022ab0c4a952b Mon Sep 17 00:00:00 2001 From: "Adrien Widart (awt)" Date: Fri, 10 Nov 2023 12:58:23 +0100 Subject: [PATCH] [FIX] base: import record using database identifier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Suppose a user who imports a new picking thanks to this: ```csv location_id,location_dest_id,picking_type_id WH/Stock,Partners/Customers,YourCompany: Delivery Orders ``` Then, on the inferface, for one of the fields, he sets the value as "Database ID" (for instance "Destination Location/Database ID"). When trying to import the file, an error is raised, which could make sense since the provided value is not a DB identifier, but the error is actually incorrect: > Odoo Server Error. Current transaction is aborted, commands ignored > until end of transaction block When looking for the destination location in the database, it will raise a legit error since the domain does not make sense: `[('id', '=', tentative_id)]` Hence this: > ERROR: invalid input syntax for type integer: "Partners/Customers" The good point is that everything in the code already handles this: we catch the error and add some detailed explanations in the import error report, but... The SQL transaction is now broken. So, as soon as another SQL request is executed, it will trigger an `InFailedSqlTransaction` error, we will not catch it and this error will be returned to the frontend (we lose the import report). sentry-3969379125 closes odoo/odoo#143010 X-original-commit: b0e86e5c6d4b54134b91dbde3a145b551158b76d Signed-off-by: Rémy Voet (ryv) --- odoo/addons/base/models/ir_fields.py | 10 ++++------ odoo/addons/test_impex/tests/test_load.py | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/odoo/addons/base/models/ir_fields.py b/odoo/addons/base/models/ir_fields.py index b258d553bf2..fa0ee7f200f 100644 --- a/odoo/addons/base/models/ir_fields.py +++ b/odoo/addons/base/models/ir_fields.py @@ -432,18 +432,16 @@ class IrFieldsConverter(models.AbstractModel): field_type = _(u"database id") if isinstance(value, str) and not self._str_to_boolean(model, field, value)[0]: return False, field_type, warnings - try: tentative_id = int(value) - except ValueError: tentative_id = value try: - if RelatedModel.search([('id', '=', tentative_id)]): - id = tentative_id - except psycopg2.DataError: - # type error + tentative_id = int(value) + except ValueError: raise self._format_import_error( ValueError, _(u"Invalid database id '%s' for the field '%%(field)s'"), value, {'moreinfo': action}) + if RelatedModel.browse(tentative_id).exists(): + id = tentative_id elif subfield == 'id': field_type = _(u"external id") if not self._str_to_boolean(model, field, value)[0]: diff --git a/odoo/addons/test_impex/tests/test_load.py b/odoo/addons/test_impex/tests/test_load.py index 00a25ab0f4b..4dfb344bce8 100644 --- a/odoo/addons/test_impex/tests/test_load.py +++ b/odoo/addons/test_impex/tests/test_load.py @@ -706,6 +706,13 @@ class test_m2o(ImporterCase): u"name, external id or database id")]) self.assertIs(result['ids'], False) + def test_fail_id(self): + record = self.env['export.integer'].create({'value': 42}) + result = self.import_(['value/.id'], [["lorem"], [record.id]]) + self.assertFalse(result['ids']) + self.assertTrue(result['messages']) + self.assertEqual(result['messages'][0]['message'], "Invalid database id 'lorem' for the field 'Value'") + def test_name_create_enabled_m2o(self): result = self.import_(['value'], [[101]]) self.assertEqual(result['messages'], [message(