mirror of
https://github.com/odoo/documentation.git
synced 2026-03-27 14:10:23 +07:00
[IMP] core: SQLObject replaces _sql_constraints
task-3390431 closes odoo/documentation#11071 Related: odoo/odoo#175783 Related: odoo/enterprise#68589 Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
===
|
||||
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user