尝试构建数据库时出现Phoenix unique_constraint错误

问题描述 投票:1回答:1

我正在关注Phoenix 1.4的书,当我试图通过终端向数据库输入数据时,我得到了“使用存储库数据”部分,我收到了一个错误。

我的IEx会议:

iex(1)> alias Rumbl.Repo
Rumbl.Repo
iex(2)> alias Rumbl.Accounts.User
Rumbl.Accounts.User
iex(3)> Repo.insert(%User{
...(3)> name: "José", username: "josevalim"
...(3)> })
[debug] QUERY ERROR db=16.7ms queue=1.2ms
INSERT INTO "users" ("name","username","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["José", "josevalim", ~N[2018-11-24 11:47:30], ~N[2018-11-24 11:47:30]]

它引发了以下错误:

** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * users_username_index (unique_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `unique_constraint/3` on your changeset with the constraint
`:name` as an option.

The changeset has not defined any constraint.

    (ecto) lib/ecto/repo/schema.ex:641: anonymous fn/4 in Ecto.Repo.Schema.constraints_to_errors/3
    (elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/ecto/repo/schema.ex:626: Ecto.Repo.Schema.constraints_to_errors/3
    (ecto) lib/ecto/repo/schema.ex:238: anonymous fn/15 in Ecto.Repo.Schema.do_insert/3
elixir phoenix-framework ecto
1个回答
1
投票

您似乎已在unique index表迁移中创建了users

约束在你的username键上,这意味着users表中的两个记录不能具有相同的用户名。在您的情况下,这意味着您已使用用户名josevalim在表中创建了另一个用户。


要让Ecto返回{:error, changeset}元组而不是引发错误,你可以告诉架构你的unique constraint方法中有一个changeset/2

def changeset(user, params) do
  user
  |> cast(params, [:name, :username])
  |> validate_required([:name, :username])
  |> unique_constraint(:username, name: :users_username_index)
end

并在插入任何内容之前使用此方法构建变更集:

%User{}
|> changeset(%{name: "José", username: "josevalim"})
|> Repo.insert
© www.soinside.com 2019 - 2024. All rights reserved.