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 Sep 23, 2024
1 parent 4df1b8e commit ce2df4e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 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 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``)
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
22 changes: 21 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 @@ -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
==========

Expand Down
5 changes: 5 additions & 0 deletions content/developer/reference/backend/orm/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Changelog
=========

Odoo Online version 18.1
========================

- Declare constraints and indexes as model attributes with `#175783 <https://github.com/odoo/odoo/pull/175783>`_.

Odoo version 18.0
=================

Expand Down
13 changes: 7 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,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

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,12 +33,10 @@ 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.

TODO update this example
You can find a simple example
`here <https://github.com/odoo/odoo/blob/24b0b6f07f65b6151d1d06150e376320a44fd20a/addons/analytic/models/analytic_account.py#L20-L23>`__.

Expand Down

0 comments on commit ce2df4e

Please sign in to comment.