Hello,
add_constraint()
that you can find in Ecto.2.2 ecto/lib/ecto/changeset.ex at the line 2235 should add a field constraints:
to the changeset containing a list of constraints, right?
However I don’t see this field in the changeset when I apply Changeset.unique_constraint()
#Ecto.Changeset<action: nil, changes: %{}, errors: [],
data: #App.CMS.Author<>, valid?: true>
but when I insert the changeset with an user_id already created, I got the correct ecto error message:
#Ecto.Changeset<action: :insert, changes: %{user_id: 1},
errors: [user_id: {"has already been taken", []}],
data: #App.CMS.Author<>, valid?: false>}
Here the data field of the changeset (if it could help)
%App.CMS.Author{__meta__: #Ecto.Schema.Metadata<:built, "authors">,
bio: nil, genre: nil, id: nil, inserted_at: nil,
pages: #Ecto.Association.NotLoaded<association :pages is not loaded>,
role: nil, updated_at: nil,
user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1}
And the migration
def change do
create table(:authors) do
add :bio, :text
add :role, :string
add :genre, :string
add :user_id, references(:users, on_delete: :delete_all),
null: false
timestamps()
end
create unique_index(:authors, [:user_id])
end
HOW does it work?