2021-04-01 19:19:30 +13:00
|
|
|
defmodule AshPostgres.Reference do
|
2022-08-24 11:56:46 +12:00
|
|
|
@moduledoc false
|
2022-05-24 18:11:54 +12:00
|
|
|
defstruct [:relationship, :on_delete, :on_update, :name, ignore?: false]
|
2021-04-01 19:19:30 +13:00
|
|
|
|
|
|
|
def schema do
|
|
|
|
[
|
|
|
|
relationship: [
|
|
|
|
type: :atom,
|
|
|
|
required: true,
|
|
|
|
doc: "The relationship to be configured"
|
|
|
|
],
|
2022-05-24 18:11:54 +12:00
|
|
|
ignore?: [
|
|
|
|
type: :boolean,
|
|
|
|
doc:
|
|
|
|
"If set to true, no reference is created for the given relationship. This is useful if you need to define it in some custom way"
|
|
|
|
],
|
2021-04-01 19:19:30 +13:00
|
|
|
on_delete: [
|
|
|
|
type: {:one_of, [:delete, :nilify, :nothing, :restrict]},
|
|
|
|
doc: """
|
|
|
|
What should happen to records of this resource when the referenced record of the *destination* resource is deleted.
|
|
|
|
|
|
|
|
The difference between `:nothing` and `:restrict` is subtle and, if you are unsure, choose `:nothing` (the default behavior).
|
|
|
|
`:restrict` will prevent the deletion from happening *before* the end of the database transaction, whereas `:nothing` allows the
|
|
|
|
transaction to complete before doing so. This allows for things like deleting the destination row and *then* deleting the source
|
|
|
|
row.
|
|
|
|
|
|
|
|
## Important!
|
|
|
|
|
|
|
|
No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued.
|
|
|
|
This operation happens *directly* in the database.
|
|
|
|
|
|
|
|
This option is called `on_delete`, instead of `on_destroy`, because it is hooking into the database level deletion, *not*
|
|
|
|
a `destroy` action in your resource.
|
|
|
|
"""
|
|
|
|
],
|
|
|
|
on_update: [
|
|
|
|
type: {:one_of, [:update, :nilify, :nothing, :restrict]},
|
|
|
|
doc: """
|
2022-08-19 06:56:36 +12:00
|
|
|
What should happen to records of this resource when the referenced destination_attribute of the *destination* record is update.
|
2021-04-01 19:19:30 +13:00
|
|
|
|
|
|
|
The difference between `:nothing` and `:restrict` is subtle and, if you are unsure, choose `:nothing` (the default behavior).
|
|
|
|
`:restrict` will prevent the deletion from happening *before* the end of the database transaction, whereas `:nothing` allows the
|
|
|
|
transaction to complete before doing so. This allows for things like updating the destination row and *then* updating the reference
|
|
|
|
as long as you are in a transaction.
|
|
|
|
|
|
|
|
## Important!
|
|
|
|
|
|
|
|
No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued.
|
|
|
|
This operation happens *directly* in the database.
|
|
|
|
"""
|
|
|
|
],
|
|
|
|
name: [
|
|
|
|
type: :string,
|
|
|
|
doc:
|
2022-08-19 06:56:36 +12:00
|
|
|
"The name of the foreign key to generate in the database. Defaults to <table>_<source_attribute>_fkey"
|
2021-04-01 19:19:30 +13:00
|
|
|
]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|