【问题标题】:unique_constraint on 2 fields not being recognized2 个字段上的 unique_constraint 未被识别
【发布时间】:2015-12-13 22:04:23
【问题描述】:

我正在尝试在 Ecto Postgres 中执行 2 个字段唯一约束。到目前为止,我设法做到了:

迁移中:

create unique_index(:presences, [:event_id, :user_id], name: :special_event_user)

变更集:

 def changeset(presence, params \\ :empty) do
    presence
      |> cast(params, @required_fields, @optional_fields)
      |> foreign_key_constraint(:user_id)
      |> foreign_key_constraint(:event_id)
      |> unique_constraint(:event_id, name: :special_event_user)
  end

也试过了:

|> unique_constraint(:special_event_user)

但我不断得到:

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.

我以为我已经做到了。有什么建议吗?

编辑:

行动:

def assign(conn, %{"event" => event_id}) do
    case Integer.parse(event_id) do
      {val, _ } ->
          case Repo.one(User.unique_user(User, get_session(conn, :login))) do
            nil -> conn
              |> put_flash(:error, "Błąd bazy danych")
              |> redirect(to: "/event")
            result ->
              presence = %Presence{ user_id: result.id, event_id: val, state: Presence.get_assigned } |> Repo.insert!
              conn
                |> put_flash(:info, "Zostałeś zapisany na wydarzenie")
                |> redirect(to: "/event")
          end
    end
  end

上面代码中没有错误时的示例:

%Kpsz.Model.Presence{__meta__: #Ecto.Schema.Metadata<:loaded>,
 event: #Ecto.Association.NotLoaded<association :event is not loaded>,
 event_id: 1, id: 1, inserted_at: #Ecto.DateTime<2015-12-14T15:45:39Z>,
 state: 1, updated_at: #Ecto.DateTime<2015-12-14T15:45:39Z>,
 user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1}

编辑:现在我得到:

constraint error when attempting to insert model:

    * unique: special_event_user

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: presences_special_event_user_index
    * foreign_key: presences_event_id_fkey
    * foreign_key: presences_user_id_fkey

【问题讨论】:

  • 您的定义似乎与 Ecto 文档中的示例相匹配。尝试将索引名称直接传递到约束中,就像在 blog 中所做的那样,即 unique_constraint(:special_event_user)
  • 对我来说也一样...
  • 你能提供你用来创建记录的代码吗?
  • @Gazler 完成。抱歉耽搁了。

标签: elixir phoenix-framework ecto


【解决方案1】:

你没有通过你的变更集函数。

presence =
  %Presence{ user_id: result.id, event_id: val, state: Presence.get_assigned }
  |> Repo.insert!

应该是:

presence =
  Presence.changeset(%Presence{}, %{user_id: result.id, event_id: val, state: Presence.get_assigned})
  |> Repo.insert!

【讨论】:

  • 嗯.. 盲目的复制有时会很痛苦。非常感谢您抽出宝贵的时间,先生。
  • 可以再看看主帖底部吗?我遇到了另一个奇怪的错误。它仍然告诉我我还没有调用那个函数。但是这次它会提示调用函数的名称,其中一个与我使用的非常相似。
  • 如果您遇到约束错误并使用Repo.insert! 而不是Repo.insert,那么它会引发。这可能是正在发生的事情吗?
  • 我正在使用 Repo.insert,我已经删除了“!”从复制的代码中。
  • 你在用unique_constraint(:event_id, name: :special_event_user)吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 2017-02-12
  • 2012-02-28
  • 2019-06-05
  • 2013-04-10
相关资源
最近更新 更多