gpt4 book ai didi

ruby-on-rails - PG::ForeignKeyViolation: 将 Rails 中的 has_and_belongs_to_many 关联更改为 has_many :through 关联后出错

转载 作者:行者123 更新时间:2023-11-28 21:36:43 28 4
gpt4 key购买 nike

我最近将 has_and_belongs_to_many (HABTM) 关联切换为 has_many :through 关联,现在由于违反外键约束,我有很多失败的测试。这是一个示例测试失败消息:

DRb::DRbRemoteError: PG::ForeignKeyViolation: ERROR:  update or delete on table "choices" violates foreign key constraint "fk_rails_d6ffbc38aa" on table "selections"
DETAIL: Key (id)=(506907318) is still referenced from table "selections"

这是创建关联的表单。 screenshot of my form

这是我与相关协会的模型......

models/variant.rb

class Variant < ApplicationRecord    
has_many :selections
has_many :choices, through: :selections
end

models/choice.rb

class Choice < ApplicationRecord
has_many :variants, through: :selections

belongs_to :option
end

models/selection.rb

class Selection < ApplicationRecord
belongs_to :choice
belongs_to :variant
end

以及相关的模式:

create_table "variants", force: :cascade do |t|
t.string "sku"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "account_id"
t.bigint "product_id"
t.index ["account_id"], name: "index_variants_on_account_id"
t.index ["product_id"], name: "index_variants_on_product_id"
end

create_table "choices", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "option_id"
t.bigint "account_id"
t.index ["account_id"], name: "index_choices_on_account_id"
t.index ["option_id"], name: "index_choices_on_option_id"
end

create_table "selections", force: :cascade do |t|
t.bigint "choice_id"
t.bigint "variant_id"
t.datetime "created_at", precision: 6
t.datetime "updated_at", precision: 6
t.index ["choice_id"], name: "index_selections_on_choice_id"
t.index ["variant_id"], name: "index_selections_on_variant_id"
end

在浏览器中一切正常。只是我的测试失败了,所有失败的测试都有同样的外键错误。

我不想开始向我的模型添加关联和外键只是为了让我的测试通过,因为预期的行为在浏览器中有效。

我正在使用固定装置进行测试。这是问题吗?我的灯具是否有什么可能导致此错误?仅供引用,我想继续使用固定装置而不是将应用程序切换到工厂。

test/fixtures/variants.yml

small_t_shirt_in_red:
id: 1
account: fuzz
product: t_shirt
sku: FUZZ-T-001
choices: small, red

medium_generic_gadget_in_blue:
id: 2
account: generic_gadgets
product: gadget
sku: GENERIC-001
choices: medium, blue


test/fixtures/choices.yml

small:
name: Small

medium:
name: Medium

large:
name: Large

red:
name: Red

blue:
name: Blue


test/fixtures/selections.yml

small_t_shirt_selection:
choice_id: 1
variant_id: 1

red_t_shirt_selection:
choice_id: 4
variant_id: 1

medium_generic_selection:
choice_id: 2
variant_id: 2

blue_generic_selection:
choice_id: 5
variant_id: 2

最佳答案

尝试删除并重新创建您的测试数据库:

RAILS_ENV=test rails db:drop db:create

也许测试数据库中有一个主/开发数据库没有的外键约束。

关于ruby-on-rails - PG::ForeignKeyViolation: 将 Rails 中的 has_and_belongs_to_many 关联更改为 has_many :through 关联后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58044212/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com