【问题标题】:Ecto constraint error on different schema不同模式上的 Ecto 约束错误
【发布时间】:2019-02-03 10:04:31
【问题描述】:

我很确定我在这里遗漏了一些东西,但我正在为此烦恼。我有以下架构的设置:

schema "accounts_providers" do
  field :provider, :string, null: false
  field :uid, :string, null: false
  field :auth_token, Persistence.Encrypted.Binary, null: false

  belongs_to :user, MyApp.Accounts.User
  timestamps()
end
schema "accounts_users" do
  field :name, :string
  field :username, :string
  field :nickname, :string
  field :email, :string
  field :avatar_url, :string

  has_many :providers, Polydoc.Accounts.Provider, on_delete: :delete_all
  timestamps()
end
schema "repositories_repositories" do
  field :name, :string
  field :description, :string
  field :fork, :boolean
  field :private, :boolean
  field :write_permission, :boolean
  field :uid, :string
  field :owner_name, :string

  belongs_to :provider, Polydoc.Accounts.Provider
end

我正在尝试将一条记录插入到 accounts_providers 表中,但遇到了约束错误,但该错误引用了 repositories_repositories 表中存在的约束,我认为我没有触及

p>
[debug] QUERY ERROR db=32.6ms queue=9.5ms
INSERT INTO "accounts_providers" ("auth_token","provider","uid","user_id","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT ("uid","provider") DO UPDATE SET "id" = EXCLUDED."id","provider" = EXCLUDED."provider","uid" = EXCLUDED."uid","auth_token" = EXCLUDED."auth_token","user_id" = EXCLUDED."user_id","inserted_at" = EXCLUDED."inserted_at","updated_at" = EXCLUDED."updated_at" RETURNING "id" [<<1, 10, 65, 69, 83, 46, 71, 67, 77, 46, 86, 49, 217, 158, 158, 208, 9, 91, 16, 111, 110, 135, 12, 82, 201, 8, 126, 181, 141, 227, 56, 145, 148, 2, 217, 50, 202, 36, 4, 85, 228, 160, 42, 249, 38, 24, 135, 59, 235, ...>>, "github", "1657075", 1, ~N[2019-02-03 09:42:39], ~N[2019-02-03 09:42:39]]
[info] Sent 500 in 1980ms
[error] #PID<0.586.0> running PolydocWeb.Endpoint (cowboy_protocol) terminated
Server: app.lvh.me:4000 (http)
Request: GET /auth/github/callback?code=5bf8f2761b6d3892054f
** (exit) an exception was raised:
    ** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * repositories_repositories_provider_id_fkey (foreign_key_constraint)

If you would like to stop this constraint violation from raising an
exception and instead add it as an error to your changeset, please
call `foreign_key_constraint/3` on your changeset with the constraint
`:name` as an option.

The changeset defined the following constraints:

    * accounts_providers_user_id_fkey (foreign_key_constraint)
    * accounts_providers_uid_provider_index (unique_constraint)

这是在我尝试运行 upsert 查询时触发的:

provider = Provider.oauth_changeset(%Provider{}, attrs)
Repo.insert(provider, on_conflict: :replace_all, conflict_target: [:uid, :provider])

provider 在哪里

#Ecto.Changeset<
  action: nil,
  changes: %{
    auth_token: "5ab9b61181eb3bc8221310eb4121a861ebb5b0a8",
    provider: "github",
    uid: "1657075",
    user_id: 1
  },
  errors: [],
  data: #Polydoc.Accounts.Provider<>,
  valid?: true
>

任何线索为什么我会遇到这个错误,即使约束在另一个表上?我知道我的变更集中没有foreign_key_constraint,但这对我来说并不能解释来自错误表的错误

编辑

这是 Provider 架构的变更集函数

def oauth_changeset(%__MODULE__{} = provider, attrs) do
  provider
  |> cast(attrs, [:provider, :uid, :auth_token, :user_id])
  |> validate_required([:provider, :uid, :auth_token])
  |> unique_constraint(:provider_uid, name: :accounts_providers_uid_provider_index)
  |> foreign_key_constraint(:user_id)
end

【问题讨论】:

  • 您能否也发布变更集功能。如果您将生成的 SQL 并在数据库中手动运行会发生什么?
  • @MaartenvanVliet 我已经添加了变更集功能
  • hmm 刚刚检查了 SQL,似乎 Ecto 正在尝试将记录的 id 设置为冲突,并且数据库中应该存在冲突,所以猜测,它正在尝试更新 ID到不同的东西,然后导致另一个表中的外键约束变得无效

标签: elixir ecto


【解决方案1】:

好的,这与 Ecto 处理 insert/2 函数的 on_conflict 选项的方式有关。如果像我的一样设置为:replace_all,它实际上会替换所有看起来包括主键的东西,而主键又通过使现有记录的 id 无效而破坏了我的另一个表的外键。

为了解决这个问题,我更改了插入语句以包含指定要替换的字段:

Repo.insert(provider,
            on_conflict: { :replace, [:auth_token, :updated_at]},
            conflict_target: [:uid, :provider])

【讨论】:

    【解决方案2】:

    对我来说解决办法是

    Repo.insert_or_update(provider, 
        on_conflict:[
           set: [
             auth_token: auth_token, 
             updated_at: update_at
           ]
        ], 
        conflict_target: [:uid, :provider])
    

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多