View Source EctoDiscriminator.DiscriminatorType (ecto_discriminator v0.2.6)
A custom type used by the EctoDiscriminator.Schema
to know which field should be used for its logic.
EctoDiscriminator.DiscriminatorType
field holds name of the diverged module. Field name can be anything.
User has to mark exactly one field in the base schema with this type. Diverged schemas will inherit this setting.
Forgetting to add this field will result in compilation error.
Basic setup:
field :type, EctoDiscriminator.DiscriminatorType
You can also use it as a primary key:
@primary_key {:id, EctoDiscriminator.DiscriminatorType, []}
You can configure any options that are supported by Ecto.Schema
(except virtual
and default
from obvious reasons).
field :discriminator, EctoDiscriminator.DiscriminatorType, load_in_query: false
When it comes to the migrations, the underlying type is just :string
.
That said, it's completely fine to create migration with string field:
add :type, :string
It's possible to be more restrictive and provide an enum inside DB.
Keep in mind that module name will be stored in form Elixir.Module.Name
.
Link to this section Summary
Functions
Callback implementation for Ecto.Type.cast/1
.
Callback implementation for Ecto.Type.dump/1
.
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Callback implementation for Ecto.Type.load/1
.
Callback implementation for Ecto.Type.type/0
.
Link to this section Functions
Callback implementation for Ecto.Type.cast/1
.
Callback implementation for Ecto.Type.dump/1
.
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Callback implementation for Ecto.Type.load/1
.
Callback implementation for Ecto.Type.type/0
.