23 lines
910 B
Elixir
23 lines
910 B
Elixir
|
defmodule <%= inspect schema.module %> do
|
||
|
use Ecto.Schema
|
||
|
import Ecto.Changeset
|
||
|
<%= if schema.prefix do %>
|
||
|
@schema_prefix :<%= schema.prefix %><% end %><%= if schema.binary_id do %>
|
||
|
@primary_key {:id, :binary_id, autogenerate: true}
|
||
|
@foreign_key_type :binary_id<% end %>
|
||
|
schema <%= inspect schema.table %> do
|
||
|
<%= Mix.Phoenix.Schema.format_fields_for_schema(schema) %>
|
||
|
<%= for {_, k, _, _} <- schema.assocs do %> field <%= inspect k %>, <%= if schema.binary_id do %>:binary_id<% else %>:id<% end %>
|
||
|
<% end %>
|
||
|
timestamps()
|
||
|
end
|
||
|
|
||
|
@doc false
|
||
|
def changeset(<%= schema.singular %>, attrs) do
|
||
|
<%= schema.singular %>
|
||
|
|> cast(attrs, [<%= Enum.map_join(schema.attrs, ", ", &inspect(elem(&1, 0))) %>])
|
||
|
|> validate_required([<%= Enum.map_join(schema.attrs, ", ", &inspect(elem(&1, 0))) %>])
|
||
|
<%= for k <- schema.uniques do %> |> unique_constraint(<%= inspect k %>)
|
||
|
<% end %> end
|
||
|
end
|