Referential Actions Enum Class (#735)
* feat: add action enum class to referential actions * feat: write validation func for action name string * test: write test for validation referential action * fix: backend database running for action test * fix: set the string type of value enum class * fix: debuging return statement type for validation * fix: return non empty for empty action * refactor: change in line return if statement * fix: add iterate method in read document md * fix: update foreign key docstring types * docs: write documention of refernal actions * docs: complete referential actions descriptions * refactor: rename and reposition referential action * refactor: change validate referential action func * test: add assert check for really deleted rows * fix: debug error problem in renamed enum class * fix: apply black formatted codes * docs: update the document for referential actions * docs: added note for server default argument Co-authored-by: collerek <collerek@gmail.com>
This commit is contained in:
@ -6,6 +6,7 @@ Following methods allow you to load data from the database.
|
||||
* `get_or_create(_defaults: Optional[Dict[str, Any]] = None, *args, **kwargs) -> Tuple[Model, bool]`
|
||||
* `first(*args, **kwargs) -> Model`
|
||||
* `all(*args, **kwargs) -> List[Optional[Model]]`
|
||||
* `iterate(*args, **kwargs) -> AsyncGenerator[Model]`
|
||||
|
||||
|
||||
* `Model`
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
# ForeignKey
|
||||
|
||||
`ForeignKey(to, related_name=None)` has required parameters `to` that takes target `Model` class.
|
||||
`ForeignKey(to: Model, *, name: str = None, unique: bool = False, nullable: bool = True,
|
||||
related_name: str = None, virtual: bool = False, onupdate: Union[ReferentialAction, str] = None,
|
||||
ondelete: Union[ReferentialAction, str] = None, **kwargs: Any)`
|
||||
has required parameters `to` that takes target `Model` class.
|
||||
|
||||
Sqlalchemy column and Type are automatically taken from target `Model`.
|
||||
|
||||
@ -182,6 +185,41 @@ But you can overwrite this name by providing `related_name` parameter like below
|
||||
the `related_name` for you. Therefore, in that situation you **have to** provide `related_name`
|
||||
for all but one (one can be default and generated) or all related fields.
|
||||
|
||||
## Referential Actions
|
||||
|
||||
When an object referenced by a ForeignKey is changed (deleted or updated),
|
||||
ormar will set the SQL constraint specified by the `ondelete` and `onupdate` argument.
|
||||
|
||||
The possible values for `ondelete` and `onupdate` are found in `ormar.ReferentialAction`:
|
||||
|
||||
!!!note
|
||||
Instead of `ormar.ReferentialAction`, you can directly pass string values to these two arguments, but this is not recommended because it will break the integrity.
|
||||
|
||||
### CASCADE
|
||||
|
||||
Whenever rows in the parent (referenced) table are deleted (or updated), the respective rows of the child (referencing) table with a matching foreign key column will be deleted (or updated) as well. This is called a cascade delete (or update).
|
||||
|
||||
### RESTRICT
|
||||
|
||||
A value cannot be updated or deleted when a row exists in a referencing or child table that references the value in the referenced table.
|
||||
|
||||
Similarly, a row cannot be deleted as long as there is a reference to it from a referencing or child table.
|
||||
|
||||
### SET_NULL
|
||||
|
||||
Set the ForeignKey to `None`; this is only possible if `nullable` is True.
|
||||
|
||||
### SET_DEFAULT
|
||||
|
||||
Set the ForeignKey to its default value; a `server_default` for the ForeignKey must be set.
|
||||
|
||||
!!!note
|
||||
Note that the `default` value is not allowed and you must do this through `server_default`, which you can read about in [this section][server_default].
|
||||
|
||||
### DO_NOTHING
|
||||
|
||||
Take `NO ACTION`; NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.
|
||||
|
||||
## Relation Setup
|
||||
|
||||
You have several ways to set-up a relationship connection.
|
||||
@ -242,4 +280,5 @@ Finally you can explicitly set it to None (default behavior if no value passed).
|
||||
[exists]: ./queries.md#exists
|
||||
[fields]: ./queries.md#fields
|
||||
[exclude_fields]: ./queries.md#exclude_fields
|
||||
[order_by]: ./queries.md#order_by
|
||||
[order_by]: ./queries.md#order_by
|
||||
[server_default]: ../fields/common-parameters.md#server-default
|
||||
Reference in New Issue
Block a user