【发布时间】:2017-07-17 19:06:21
【问题描述】:
运行此迁移以便在帐号上有唯一索引
def change do
create unique_index(:users, [:account_number])
end
然后在模型中:
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:first_name, :last_name, :email, :phone, :city, :postal_code, :country, :login_count, :last_login, :active, :account_number, :password])
|> validate_required([:first_name, :last_name, :email, :phone, :city, :postal_code, :country, :account_number])
|> validate_length(:password, min: 8, max: 100)
|> validate_format(:email, ~r/@/)
|> unique_constraint(:email)
|> unique_constraint(:account_number)
|> put_pass_hash()
end
产生这个错误:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* unique: users_account_number_index
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 has not defined any constraint.
PostgreSQL 9.6
凤凰 1.2.4
Ecto 2.1.4
我错过了什么?
【问题讨论】:
-
The changeset has not defined any constraint.很奇怪。你确定这个changeset函数在User模型中并且你正在插入一个由这个函数返回的变更集吗? -
一切都编译好了?
标签: validation elixir phoenix-framework ecto