【问题标题】:Unique index constraint not working in Phoenix app唯一索引约束在 Phoenix 应用程序中不起作用
【发布时间】:2017-01-16 18:42:32
【问题描述】:

在我的 Phoenix 应用程序中,我的用户模型如下:

defmodule MyApp.User do
  use MyApp.Web, :model

  schema "users" do
    field :username, :string, unique: true
    field :email, :string, unique: true
    field :crypted_password, :string
    field :password, :string, virtual: true

    timestamps
  end

  @required_fields ~w(email password username)
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
    |> unique_constraint(:email)
    |> unique_constraint(:username)
    |> validate_format(:email, ~r/@/)
    |> validate_length(:password, min: 5)
  end
end

我也有以下迁移:

defmodule MyApp.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :email, :string
      add :username, :string
      add :crypted_password, :string

      timestamps
    end

    create unique_index(:users, [:email])
    create unique_index(:users, [:username])
  end
end

我的registration_controller_ex 如下:

defmodule MyApp.RegistrationController do
  use MyApp.Web, :controller
  alias MyApp.User

  def new(conn, _params) do
    changeset = User.changeset(%User{})
    render conn, changeset: changeset
  end

  def create(conn, %{"user" => user_params}) do
    changeset = User.changeset(%User{}, user_params)

    if changeset.valid? do
      user = MyApp.Registration.create(changeset, MyApp.Repo)
      conn
      |> put_flash(:info, "Your account was created")
      |> redirect(to: "/")
    else
      conn
      |> put_flash(:info, "Unable to create account")
      |> render("new.html", changeset: changeset)
    end
  end
end

所以,尽管如此,我很确定我在 User 中的 usernameemail 字段是唯一索引。我还通过调用unique_constraint 来验证User.changeset 来确保它们是唯一的。但是,当我在我的界面中创建一个与以前创建的用户具有相同电子邮件和用户名的用户时,变更集被验证并且用户被“创建”。 (实际上并没有创建,当我查看数据库时没有添加任何内容)

我在我的服务器日志中得到以下信息,但我的 changeset.valid?是真的。

[debug] BEGIN [] OK query=139.3ms queue=8.2ms
[debug] INSERT INTO "users" ("crypted_password", "email", "inserted_at", "updated_at", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id" ["$2b$12$MN1YxFUGLMIJYXseZn0sjuuVs9U1jRdYtRr9D8XQsAqdh.D2sRRXa", "email@gmail.com", {{2015, 9, 30}, {11, 7, 25, 0}}, {{2015, 9, 30}, {11, 7, 25, 0}}, "username"] ERROR query=5.5ms
[debug] ROLLBACK [] OK query=0.4ms

此外,我在 User.changeset 函数中查找的其他内容(例如最小密码长度和其他内容)会报告给用户并且工作正常。只是:email:username 的唯一索引没有被报告。

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    unique_constraint 将被数据库检查,因此只有在插入记录时才会触发。调用 changeset.valid? 不会检查约束,因此在这种情况下返回 true。您需要检查 Repo 插入的返回元组并执行以下操作:

    def create(conn, %{"user" => user_params}) do
      changeset = User.changeset(%User{}, user_params)
      case  MyApp.Repo.insert changeset do
        {:ok, changeset} -> 
          conn
          |> put_flash(:info, "Your account was created")
          |> redirect(to: "/")      
        {:error, changeset} ->
          conn
          |> put_flash(:info, "Unable to create account")
          |> render("new.html", changeset: changeset)      
      end
    end
    

    现在您的changeset.errors 已丰富,您应该可以使用changeset.errors[:email] 获取错误

    【讨论】:

    • 它没有用。我有一个问题:registration_controller.ex:15: function changeset/0 undefined
    • 为了后代,上面的行应该稍微改变一下:changset = User.changeset(%User{}, user_params)
    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多