From 60001793847a73e58367ca80e8103719ef4359a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Wed, 12 Jan 2022 15:21:51 +0000 Subject: [PATCH] [REM] base: remove `_sequence` attribute of `BaseModel` The `_sequence` was only used for the insertion of data in the DB for the `id` value. But in Odoo, the `id` is always a `SERIAL`, then the PostgreSQL fill already the value by the right SQL sequence. Then, avoid doing the job of the DB by ourselves. Sadly, we need to manage the case when we create an empty record to always get a valid query. task-2735546 Part-of: odoo/odoo#82727 --- odoo/addons/base/models/ir_actions.py | 5 ----- odoo/addons/base/models/ir_actions_report.py | 1 - odoo/addons/test_new_api/tests/test_new_fields.py | 2 +- odoo/models.py | 9 ++++----- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/odoo/addons/base/models/ir_actions.py b/odoo/addons/base/models/ir_actions.py index ca3e87d4fa1..35419344907 100644 --- a/odoo/addons/base/models/ir_actions.py +++ b/odoo/addons/base/models/ir_actions.py @@ -170,7 +170,6 @@ class IrActionsActWindow(models.Model): _description = 'Action Window' _table = 'ir_act_window' _inherit = 'ir.actions.actions' - _sequence = 'ir_actions_id_seq' _order = 'name' @api.constrains('res_model', 'binding_model_id') @@ -348,7 +347,6 @@ class IrActionsActUrl(models.Model): _description = 'Action URL' _table = 'ir_act_url' _inherit = 'ir.actions.actions' - _sequence = 'ir_actions_id_seq' _order = 'name' name = fields.Char(string='Action Name', translate=True) @@ -386,7 +384,6 @@ class IrActionsServer(models.Model): _description = 'Server Actions' _table = 'ir_act_server' _inherit = 'ir.actions.actions' - _sequence = 'ir_actions_id_seq' _order = 'sequence,name' DEFAULT_PYTHON_CODE = """# Available variables: @@ -675,7 +672,6 @@ class IrActionsServer(models.Model): class IrServerObjectLines(models.Model): _name = 'ir.server.object.lines' _description = 'Server Action value mapping' - _sequence = 'ir_actions_id_seq' server_id = fields.Many2one('ir.actions.server', string='Related Server Action', ondelete='cascade') col1 = fields.Many2one('ir.model.fields', string='Field', required=True, ondelete='cascade') @@ -831,7 +827,6 @@ class IrActionsActClient(models.Model): _description = 'Client Action' _inherit = 'ir.actions.actions' _table = 'ir_act_client' - _sequence = 'ir_actions_id_seq' _order = 'name' name = fields.Char(string='Action Name', translate=True) diff --git a/odoo/addons/base/models/ir_actions_report.py b/odoo/addons/base/models/ir_actions_report.py index bfe9681e2c9..dedcca3c196 100644 --- a/odoo/addons/base/models/ir_actions_report.py +++ b/odoo/addons/base/models/ir_actions_report.py @@ -92,7 +92,6 @@ class IrActionsReport(models.Model): _description = 'Report Action' _inherit = 'ir.actions.actions' _table = 'ir_act_report_xml' - _sequence = 'ir_actions_id_seq' _order = 'name' name = fields.Char(translate=True) diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index f4ff27b3871..8cb1613bbd2 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -3402,7 +3402,7 @@ class TestFieldParametersValidation(common.TransactionCase): def insert(model, *fnames): """ Return the expected query string to INSERT the given columns. """ columns = sorted(fnames + ('create_uid', 'create_date', 'write_uid', 'write_date')) - return 'INSERT INTO "{}" ("id", {}) VALUES (nextval(%s), {}) RETURNING id'.format( + return 'INSERT INTO "{}" ({}) VALUES ({}) RETURNING id'.format( model._table, ", ".join('"{}"'.format(column) for column in columns), ", ".join('%s' for column in columns), diff --git a/odoo/models.py b/odoo/models.py index fa66eaab6b4..423d1926705 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -506,7 +506,6 @@ class BaseModel(metaclass=MetaModel): """ _table = None #: SQL table name used by model if :attr:`_auto` _table_query = None #: SQL expression of the table's content (optional) - _sequence = None #: SQL sequence to use for ID field _sql_constraints = [] #: SQL constraints [(name, sql_def, message)] _rec_name = None #: field to use for labeling records, default: ``name`` @@ -703,7 +702,6 @@ class BaseModel(metaclass=MetaModel): """ Initialize base model attributes. """ cls._description = cls._name cls._table = cls._name.replace('.', '_') - cls._sequence = None cls._log_access = cls._auto inherits = {} depends = {} @@ -716,7 +714,6 @@ class BaseModel(metaclass=MetaModel): _logger.warning("The model %s has no _description", cls._name) cls._description = base._description or cls._description cls._table = base._table or cls._table - cls._sequence = base._sequence or cls._sequence cls._log_access = getattr(base, '_log_access', cls._log_access) inherits.update(base._inherits) @@ -727,7 +724,6 @@ class BaseModel(metaclass=MetaModel): for cons in base._sql_constraints: cls._sql_constraints[cons[0]] = cons - cls._sequence = cls._sequence or (cls._table + '_id_seq') cls._sql_constraints = list(cls._sql_constraints.values()) # avoid assigning an empty dict to save memory @@ -4211,7 +4207,7 @@ Fields: for data in data_list: # determine column values stored = data['stored'] - columns = [('id', "nextval(%s)", self._sequence)] + columns = [] for name, val in sorted(stored.items()): field = self._fields[name] assert field.store @@ -4245,6 +4241,9 @@ Fields: # batch, to avoid regressions as much as possible. # # That said, we haven't closed the door completely. + if not columns: # Manage the case where we create empty record + columns = [('id', '%s', psycopg2.extensions.AsIs('DEFAULT'))] + query = "INSERT INTO {} ({}) VALUES ({}) RETURNING id".format( quote(self._table), ", ".join(quote(name) for name, fmt, val in columns),