Ecto.Repo.insert! "on_conflict: :nothing" 引发 Postgresql "syntax error at or near "ON""
Ecto.Repo.insert! with "on_conflict: :nothing" raising a Postgresql "syntax error at or near "ON""
我对 Elixir 和 Phoenix 完全陌生,目前正在阅读本书 "Programming Phoenix."
我已经读到第 7 章,我有一个名为 categories
的数据库 table,其中有一列 name
和 name
上的数据库级唯一性约束。
如果在名为 "Hello" 的类别已经存在时我 运行 下面这行,我会得到预期的 Ecto.ConstraintError
:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"})
[debug] QUERY ERROR db=3.2ms queue=4.9ms idle=9982.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) RETURNING "id" ["Hello", ~N[2020-04-18 07:05:04], ~N[2020-04-18 07:05:04]]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* categories_name_index (unique_constraint)
现在,这本书告诉我可以将选项 on_conflict: :nothing
添加到对 insert!
的调用中,这样可以防止出现错误。但实际发生的是我收到 postgres 语法错误:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"}, on_conflict: :nothing)
** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "ON"
query: INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) ON CONFLICT DO NOTHING RETURNING "id"
(ecto_sql) lib/ecto/adapters/sql.ex:612: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto) lib/ecto/repo/schema.ex:657: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:263: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:164: Ecto.Repo.Schema.insert!/4
[debug] QUERY ERROR db=0.0ms queue=0.8ms idle=9038.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) ON CONFLICT DO NOTHING RETURNING "id" ["Hello", ~N[2020-04-18 07:05:13], ~N[2020-04-18 07:05:13]]
版本号:
- 长生不老药 1.9.4
- 凤凰 1.4.10
- Ecto 3.4.2
- Postgres 12.1
原来我弄错了我的 postgres 版本是 12.1
,这实际上是我的 psql
客户端的安装版本,而不是我的 postgres server ,这是 运行 9.4。 ON CONFLICT
是9.5加入的,所以解决方案是升级我的postgres服务器。
我对 Elixir 和 Phoenix 完全陌生,目前正在阅读本书 "Programming Phoenix."
我已经读到第 7 章,我有一个名为 categories
的数据库 table,其中有一列 name
和 name
上的数据库级唯一性约束。
如果在名为 "Hello" 的类别已经存在时我 运行 下面这行,我会得到预期的 Ecto.ConstraintError
:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"})
[debug] QUERY ERROR db=3.2ms queue=4.9ms idle=9982.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) RETURNING "id" ["Hello", ~N[2020-04-18 07:05:04], ~N[2020-04-18 07:05:04]]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* categories_name_index (unique_constraint)
现在,这本书告诉我可以将选项 on_conflict: :nothing
添加到对 insert!
的调用中,这样可以防止出现错误。但实际发生的是我收到 postgres 语法错误:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"}, on_conflict: :nothing)
** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "ON"
query: INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) ON CONFLICT DO NOTHING RETURNING "id"
(ecto_sql) lib/ecto/adapters/sql.ex:612: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto) lib/ecto/repo/schema.ex:657: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:263: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:164: Ecto.Repo.Schema.insert!/4
[debug] QUERY ERROR db=0.0ms queue=0.8ms idle=9038.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES (,,) ON CONFLICT DO NOTHING RETURNING "id" ["Hello", ~N[2020-04-18 07:05:13], ~N[2020-04-18 07:05:13]]
版本号:
- 长生不老药 1.9.4
- 凤凰 1.4.10
- Ecto 3.4.2
- Postgres 12.1
原来我弄错了我的 postgres 版本是 12.1
,这实际上是我的 psql
客户端的安装版本,而不是我的 postgres server ,这是 运行 9.4。 ON CONFLICT
是9.5加入的,所以解决方案是升级我的postgres服务器。