【发布时间】:2019-03-08 13:51:59
【问题描述】:
我正在关注 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
【问题讨论】:
-
该错误表明您在数据库中已有具有该名称和用户名的行。当你运行
Repo.all(User)或Repo.get_by(User, name: "José", username: "josevalim")时你会得到什么
标签: elixir phoenix-framework ecto