diff --git a/content/contributing/development/coding_guidelines.rst b/content/contributing/development/coding_guidelines.rst index 8a874adfaf..1670d2854e 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 objects declarations #. 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 a6fe7307ef..38d6f013eb 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 978685a86d..d774c262a2 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 @@ -510,6 +509,27 @@ behavior is desired: :class:`~odoo.fields.Many2one` :type: :class:`~odoo.addons.base.models.res_company` +SQL Objects +=========== + +Similarly to fields, you can declare :attr:`~odoo.models.SQLObject` attributes. +These offer support to 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. + +Most of these objects support a message argument to customize error handling. +It can either be a string and its translation will be provided in the internal +reflected constraint table. +Otherwise it can be a function that takes as parameter `env` or `env, diag` +where, respectively, they denote the environment and psycopg diagnostics. + + 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 87dcc9cd31..53abc95c06 100644 --- a/content/developer/reference/backend/orm/changelog.rst +++ b/content/developer/reference/backend/orm/changelog.rst @@ -4,6 +4,11 @@ Changelog ========= +Odoo Online version 18.1 +======================== + +- Declare constraints and indexes as model attributes with `#175783 `_. + Odoo version 18.0 ================= diff --git a/content/developer/tutorials/backend.rst b/content/developer/tutorials/backend.rst index 73d60684de..0b343544a6 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,10 @@ 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. +SQL objects are defined through :attr:`~odoo.models.SQLObject` attributes. +The latter is able to support: +: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..851453b7fb 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,17 @@ 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. +Any :attr:`~odoo.models.SQLObject` can be assigned to a model. -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.