From e9c7b7763e48e39988b1ce990221f7b586ef2436 Mon Sep 17 00:00:00 2001 From: Krzysztof Magusiak Date: Mon, 23 Sep 2024 14:41:41 +0000 Subject: [PATCH] [IMP] core: SQLObject replaces _sql_constraints task-3390431 --- .../development/coding_guidelines.rst | 3 ++- content/developer/howtos/translations.rst | 2 -- content/developer/reference/backend/orm.rst | 25 ++++++++++++++++++- .../reference/backend/orm/changelog.rst | 1 + content/developer/tutorials/backend.rst | 12 ++++----- .../server_framework_101/10_constraints.rst | 18 +++++++------ 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/content/contributing/development/coding_guidelines.rst b/content/contributing/development/coding_guidelines.rst index 8a874adfaf..2cdc634081 100644 --- a/content/contributing/development/coding_guidelines.rst +++ b/content/contributing/development/coding_guidelines.rst @@ -854,9 +854,10 @@ Symbols and Conventions at the beginning of the method. - In a Model attribute order should be - #. Private attributes (``_name``, ``_description``, ``_inherit``, ``_sql_constraints``, ...) + #. Private attributes (``_name``, ``_description``, ``_inherit``, ...) #. Default method and ``default_get`` #. Field declarations + #. SQL constraints and indexes #. Compute, inverse and search methods in the same order as field declaration #. Selection method (methods used to return computed values for selection fields) #. Constrains methods (``@api.constrains``) and onchange methods (``@api.onchange``) diff --git a/content/developer/howtos/translations.rst b/content/developer/howtos/translations.rst index 4622e75c61..572c977783 100644 --- a/content/developer/howtos/translations.rst +++ b/content/developer/howtos/translations.rst @@ -63,8 +63,6 @@ Odoo automatically exports translatable strings from "data"-type content: * if ``selection`` is present and a list (or tuple), it's exported * if their ``translate`` attribute is set to ``True``, all of their existing values (across all records) are exported -* help/error messages of :attr:`~odoo.models.Model._constraints` and - :attr:`~odoo.models.Model._sql_constraints` are exported Explicit exports ================ diff --git a/content/developer/reference/backend/orm.rst b/content/developer/reference/backend/orm.rst index d417256adb..638cd9d728 100644 --- a/content/developer/reference/backend/orm.rst +++ b/content/developer/reference/backend/orm.rst @@ -63,7 +63,6 @@ value:: Defaults to whatever value was set for :attr:`~._auto`. .. autoattribute:: _table - .. autoattribute:: _sql_constraints .. autoattribute:: _register .. autoattribute:: _abstract @@ -509,6 +508,30 @@ behavior is desired: :class:`~odoo.fields.Many2one` :type: :class:`~odoo.addons.base.models.res_company` +Constraints and indexes +======================= + +Similarly to fields, you can declare +:attr:`~odoo.models.Constraint`, +:attr:`~odoo.models.Index` and :attr:`~odoo.models.UniqueIndex`. +The name of the attribute must begin with `_` to avoid name clashes with field +names. + +You can customize error messages. +They can either be strings and their translation will be provided in the internal +reflected constraint table. +Otherwise, they can be functions that take `(env, diag)` as parameters +which respectively denote the environment and psycopg diagnostics. + +.. example:: + + .. code-block:: python + + class AModel(models.Model): + _name = 'a.model' + _my_check = models.Constraint("CHECK (x > y)", "x > y is not true") + _name_idx = models.Index("(last_name, first_name)") + Recordsets ========== diff --git a/content/developer/reference/backend/orm/changelog.rst b/content/developer/reference/backend/orm/changelog.rst index d945ae0fbf..6614540700 100644 --- a/content/developer/reference/backend/orm/changelog.rst +++ b/content/developer/reference/backend/orm/changelog.rst @@ -7,6 +7,7 @@ Changelog Odoo Online version 18.1 ======================== +- Declare constraints and indexes as model attributes with `#175783 `_. - The `json` controllers have been renamed to `jsonrpc`. They are called the same, only the `type` in the python files changed. See `#183636 `_. diff --git a/content/developer/tutorials/backend.rst b/content/developer/tutorials/backend.rst index 73d60684de..18dc3bceeb 100644 --- a/content/developer/tutorials/backend.rst +++ b/content/developer/tutorials/backend.rst @@ -797,7 +797,9 @@ Model constraints Odoo provides two ways to set up automatically verified invariants: :func:`Python constraints ` and -:attr:`SQL constraints `. +:attr:`SQL constraints `. +In a similar way, you can add more complex +:attr:`SQL indexes `. A Python constraint is defined as a method decorated with :func:`~odoo.api.constrains`, and invoked on a recordset. The decorator @@ -819,11 +821,9 @@ raise an exception if its invariant is not satisfied:: Add a constraint that checks that the instructor is not present in the attendees of his/her own session. -SQL constraints are defined through the model attribute -:attr:`~odoo.models.Model._sql_constraints`. The latter is assigned to a list -of triples of strings ``(name, sql_definition, message)``, where ``name`` is a -valid SQL constraint name, ``sql_definition`` is a table_constraint_ expression, -and ``message`` is the error message. +Constraints and indexes are defined using: +:attr:`~odoo.models.Constraint`, +:attr:`~odoo.models.Index` and :attr:`~odoo.models.UniqueIndex`. .. exercise:: Add SQL constraints diff --git a/content/developer/tutorials/server_framework_101/10_constraints.rst b/content/developer/tutorials/server_framework_101/10_constraints.rst index b6f5f771ef..2e10e0890a 100644 --- a/content/developer/tutorials/server_framework_101/10_constraints.rst +++ b/content/developer/tutorials/server_framework_101/10_constraints.rst @@ -9,7 +9,7 @@ users from setting a negative expected price. Odoo provides two ways to set up automatically verified invariants: :func:`Python constraints ` and -:attr:`SQL constraints `. +:attr:`SQL constraints `. SQL === @@ -33,14 +33,16 @@ SQL :align: center :alt: Constraints on names -SQL constraints are defined through the model attribute -:attr:`~odoo.models.Model._sql_constraints`. This attribute is assigned a list -of triples containing strings ``(name, sql_definition, message)``, where ``name`` is a -valid SQL constraint name, ``sql_definition`` is a table_constraint_ expression -and ``message`` is the error message. +SQL objects are defined through :attr:`~odoo.models.Constraint` attributes. -You can find a simple example -`here `__. +Simple example: + +.. code-block:: python + + _check_percentage = models.Constraint( + 'CHECK(percentage >= 0 AND percentage <= 100)', + 'The percentage of an analytic distribution should be between 0 and 100.', + ) .. exercise:: Add SQL constraints.