Postgresql 混淆约束错误的错误消息
Ecto向我抛出以下错误:Postgresql 混淆约束错误的错误消息,postgresql,elixir,phoenix-framework,ecto,Postgresql,Elixir,Phoenix Framework,Ecto,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_const
** (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)
changeset |> unique_constraint(:login)
所以\u键
而不是\u索引
我按如下方式调用约束函数:
Indexes:
"res_users_pkey" PRIMARY KEY, btree (id)
"res_users_login_key" UNIQUE CONSTRAINT, btree (login)
changeset |> unique_constraint(:login)
那么,我该如何实现这一点呢?当一个约束名称没有给出时,Ecto使用的约束名称是
{table\u name}}{field}\u index
。您的表可能命名为res\u users
,字段为login
,这就是为什么Ecto使用约束名称res\u users\u login\u index
,这在您的情况下是不正确的。您需要在调用unique\u约束
中明确指定名称res\u users\u login\u key
:
|> unique_constraint(:login, [name: :res_users_login_key])
请尝试
|>唯一约束(:login,[name::res\u users\u login\u index])
。您的意思是:|>唯一约束(:login,[name::res\u users\u login\u key])
?哦,是的,\u key
\u index
是默认情况下使用的不正确的名称,因为您没有指定名称
。查看\u index
似乎是不合适的默认名称,不是吗?似乎\u键
更适合唯一约束。它们可能默认为唯一约束,因为默认情况下,在异位迁移中唯一索引
为索引名使用\u索引
后缀。