gpt4 book ai didi

postgresql - 关于约束错误的令人困惑的错误消息

转载 作者:行者123 更新时间:2023-11-29 12:03:13 24 4
gpt4 key购买 nike

Ecto 向我抛出以下错误:

** (exit) an exception was raised:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

* unique: res_users_login_key

If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:

* unique: res_users_login_index

我是否正确理解了 postgresql 中约束的实际名称决定了 unique_constraint/3 函数是否成功?仅供引用,在 postgreSQL 中,约束定义如下:

Indexes:
"res_users_pkey" PRIMARY KEY, btree (id)
"res_users_login_key" UNIQUE CONSTRAINT, btree (login)

所以 _key 而不是 _index

我调用约束函数如下:

 changeset |> unique_constraint(:login)

那么,我该怎么做呢?

最佳答案

Ecto 在未给出时使用的约束名称是#{table_name}_#{field}_index。您的表可能名为 res_users,字段为 login,这就是 Ecto 使用约束名称 res_users_login_index 的原因,这在您的情况下是不正确的.您需要在调用 unique_constraint 时明确指定名称 res_users_login_key:

|> unique_constraint(:login, [name: :res_users_login_key])

关于postgresql - 关于约束错误的令人困惑的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43682469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com