Skip to content

Commit

Permalink
[IMP] core: SQLObject replaces _sql_constraints
Browse files Browse the repository at this point in the history
task-3390431
  • Loading branch information
kmagusiak committed Nov 7, 2024
1 parent 7d8368e commit e9c7b77
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 deletions.
3 changes: 2 additions & 1 deletion content/contributing/development/coding_guidelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``)
Expand Down
2 changes: 0 additions & 2 deletions content/developer/howtos/translations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
================
Expand Down
25 changes: 24 additions & 1 deletion content/developer/reference/backend/orm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ value::
Defaults to whatever value was set for :attr:`~._auto`.

.. autoattribute:: _table
.. autoattribute:: _sql_constraints

.. autoattribute:: _register
.. autoattribute:: _abstract
Expand Down Expand Up @@ -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
==========

Expand Down
1 change: 1 addition & 0 deletions content/developer/reference/backend/orm/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
Odoo Online version 18.1
========================

- Declare constraints and indexes as model attributes with `#175783 <https://github.com/odoo/odoo/pull/175783>`_.
- The `json` controllers have been renamed to `jsonrpc`. They are called the same, only the
`type` in the python files changed. See `#183636 <https://github.com/odoo/odoo/pull/183636>`_.

Expand Down
12 changes: 6 additions & 6 deletions content/developer/tutorials/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,9 @@ Model constraints

Odoo provides two ways to set up automatically verified invariants:
:func:`Python constraints <odoo.api.constrains>` and
:attr:`SQL constraints <odoo.models.Model._sql_constraints>`.
:attr:`SQL constraints <odoo.models.Constraint>`.
In a similar way, you can add more complex
:attr:`SQL indexes <odoo.models.Index>`.

A Python constraint is defined as a method decorated with
:func:`~odoo.api.constrains`, and invoked on a recordset. The decorator
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ users from setting a negative expected price.

Odoo provides two ways to set up automatically verified invariants:
:func:`Python constraints <odoo.api.constrains>` and
:attr:`SQL constraints <odoo.models.Model._sql_constraints>`.
:attr:`SQL constraints <odoo.models.Constraint>`.

SQL
===
Expand All @@ -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 <https://github.com/odoo/odoo/blob/24b0b6f07f65b6151d1d06150e376320a44fd20a/addons/analytic/models/analytic_account.py#L20-L23>`__.
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.

Expand Down

0 comments on commit e9c7b77

Please sign in to comment.