Ruby on rails PG::ForeignKeyViolation:错误:插入或更新表“&引用;违反外键约束

Ruby on rails PG::ForeignKeyViolation:错误:插入或更新表“&引用;违反外键约束,ruby-on-rails,postgresql,pg,Ruby On Rails,Postgresql,Pg,RubyonRails 下面是开发mySQL的工作 但在生产PG(部署到Heroku、postgreSQL)上出现错误,并且只针对某些记录 我所拥有的: 汽车尺寸(例如S、M、L) 汽车等级(例如,新的、普通的、旧的) 尺寸和等级制造差异汽车组(使用有很多种),本例中为18组 然后汽车属于不同的群体 当我试着发射下面的命令时,它可以正常工作6次 Car.create(reg_no: "001", group_id: 1) Car.create(reg_no: "002", group_id:

RubyonRails

下面是开发mySQL的工作

但在生产PG(部署到Heroku、postgreSQL)上出现错误,并且只针对某些记录

我所拥有的:

  • 汽车尺寸(例如S、M、L)
  • 汽车等级(例如,新的、普通的、旧的)
  • 尺寸等级制造差异汽车组(使用有很多种),本例中为18组
  • 然后汽车属于不同的群体
当我试着发射下面的命令时,它可以正常工作6次

Car.create(reg_no: "001", group_id: 1)
Car.create(reg_no: "002", group_id: 2)
...
但是

而其他人最终会:

PG::ForeignKeyViolation: ERROR:  insert or update on table "cars" violates foreign key constraint "fk_rails_fa6b5abc5a"
DETAIL:  Key (group_id)=(7) is not present in table "sizes".
因此,不接受6之后的所有组id。我不明白是什么使他们不同

组id 7由尺寸:M和等级:New组成,两者均显示在相应的表格中

irb(main):001:0>Group.find(7)
D, [2018-06-07T14:34:34.802158 #4] DEBUG -- :   Group Load (1.3ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
=> #<Group id: 7, size_id: 3, grade_id: 1, created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">
irb(main):002:0> Size.find(3)
D, [2018-06-07T14:35:59.133721 #4] DEBUG -- :   Size Load (1.3ms)  SELECT  "sizes".* FROM "sizes" WHERE "sizes"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
=> #<Size id: 3, name: "M", created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">
irb(main):004:0> Grade.find(1)
D, [2018-06-07T14:36:30.558192 #4] DEBUG -- :   Grade Load (1.4ms)  SELECT  "grades".* FROM "grades" WHERE "grades"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
=> #<Grade id: 1, name: "New", created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">
这是服务器日志中的更多跟踪详细信息,上次创建成功,第一次出现错误:

D, [2018-06-07T15:21:00.464588 #4] DEBUG -- :    (1.8ms)  COMMIT
D, [2018-06-07T15:21:00.466149 #4] DEBUG -- :   Group Load (1.1ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."size_id" = $1 AND "groups"."grade_id" = $2 LIMIT $3  [["size_id", 2], ["grade_id", 1], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.467749 #4] DEBUG -- :    (1.0ms)  BEGIN
D, [2018-06-07T15:21:00.469748 #4] DEBUG -- :   Group Load (1.1ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.472005 #4] DEBUG -- :   Car Exists (1.2ms)  SELECT  1 AS one FROM "cars" WHERE LOWER("cars"."reg_no") = LOWER($1) LIMIT $2  [["reg_no", "ขก685"], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.474472 #4] DEBUG -- :   Car Create (1.4ms)  INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["reg_no", "ขก685"], ["group_id", 4], ["created_at", "2018-06-07 15:21:00.472251"], ["updated_at", "2018-06-07 15:21:00.472251"]]
D, [2018-06-07T15:21:00.476690 #4] DEBUG -- :    (1.8ms)  COMMIT

D, [2018-06-07T15:21:00.478864 #4] DEBUG -- :   Group Load (1.7ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."size_id" = $1 AND "groups"."grade_id" = $2 LIMIT $3  [["size_id", 3], ["grade_id", 1], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.480511 #4] DEBUG -- :    (1.0ms)  BEGIN
D, [2018-06-07T15:21:00.482638 #4] DEBUG -- :   Group Load (1.2ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.485111 #4] DEBUG -- :   Car Exists (1.4ms)  SELECT  1 AS one FROM "cars" WHERE LOWER("cars"."reg_no") = LOWER($1) LIMIT $2  [["reg_no", "ขก681"], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.488164 #4] DEBUG -- :   Car Create (1.9ms)  INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["reg_no", "ขก681"], ["group_id", 7], ["created_at", "2018-06-07 15:21:00.485387"], ["updated_at", "2018-06-07 15:21:00.485387"]]
D, [2018-06-07T15:21:00.489452 #4] DEBUG -- :    (1.1ms)  ROLLBACK
rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "cars" violates foreign key constraint "fk_rails_fa6b5abc5a"
DETAIL:  Key (group_id)=(7) is not present in table "sizes".
: INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:603:in `async_exec'
...

我在开发中切换到PG,所以它会产生相同的错误

我找不到如何修复当前架构。可能是因为在开发过程中我创建了表和列,然后更改了列和表的名称。即使最后的模式看起来也不错,但可能在某些阶段外键没有正确更改。当我检查我的迁移时,我唯一的怀疑就是重命名列,即使这个方法也会更改相应的索引

所以我写了新的迁移,包括所有必需的细节,甚至它生成的模式与这里列出的原始模式相同,但它有所帮助

搜索期间发现的有趣的迁移方法:

add_foreign_key
remove_foreign_key
但我帮不了你

新生成的架构:

ActiveRecord::Schema.define(version: 2018_06_09_132438) do

  create_table "cars", force: :cascade do |t|
    t.string "reg_no"
    t.integer "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_cars_on_group_id"
    t.index ["reg_no"], name: "index_cars_on_reg_no", unique: true
  end

  create_table "grades", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "groups", force: :cascade do |t|
    t.integer "size_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["grade_id"], name: "index_groups_on_grade_id"
    t.index ["size_id", "grade_id"], name: "index_groups_on_size_id_and_grade_id", unique: true
    t.index ["size_id"], name: "index_groups_on_size_id"
  end

  create_table "prices", force: :cascade do |t|
    t.integer "group_id"
    t.integer "season_id"
    t.integer "amount"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_prices_on_group_id"
    t.index ["season_id"], name: "index_prices_on_season_id"
  end

  create_table "seasons", force: :cascade do |t|
    t.string "name"
    t.integer "start_day"
    t.integer "start_month"
    t.integer "end_day"
    t.integer "end_month"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "sizes", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "name", default: "", null: false
    t.string "email", default: "", null: false
    t.integer "role", null: false
    t.string "encrypted_password", default: "", null: false
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
  end

end

我们遇到了同样的问题

您需要的是检查schema.rb并查看按钮


您将看到以前使用外键创建的迁移,您必须再次更新它并运行迁移。

您可以将Group.find(7)的输出添加到问题中吗?我不建议在MySQL上开发并部署到Postgres。相比之下,MySQL非常松散,您会遇到MySQL允许在生产中出错的模糊查询的问题。在本地设置postgres并不难。@xploshioOn-done,还添加了相应大小和大小的查找结果Group@max也许你是对的,我刚刚学过这样的ROR,所以开始像这样开发,即使是在MySQL上开发并部署到Postgres上也不推荐。这并不能消除这在Postgresql上不起作用的事实。
create_table "cars", force: :cascade do |t|
    t.string "reg_no"
    t.integer "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_cars_on_group_id"
    t.index ["reg_no"], name: "index_cars_on_reg_no", unique: true
  end

  create_table "groups", force: :cascade do |t|
    t.integer "size_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["grade_id"], name: "index_groups_on_grade_id"
    t.index ["size_id", "grade_id"], name: "index_groups_on_size_id_and_grade_id", unique: true
    t.index ["size_id"], name: "index_groups_on_size_id"
  end

  create_table "sizes", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "grades", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
D, [2018-06-07T15:21:00.464588 #4] DEBUG -- :    (1.8ms)  COMMIT
D, [2018-06-07T15:21:00.466149 #4] DEBUG -- :   Group Load (1.1ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."size_id" = $1 AND "groups"."grade_id" = $2 LIMIT $3  [["size_id", 2], ["grade_id", 1], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.467749 #4] DEBUG -- :    (1.0ms)  BEGIN
D, [2018-06-07T15:21:00.469748 #4] DEBUG -- :   Group Load (1.1ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.472005 #4] DEBUG -- :   Car Exists (1.2ms)  SELECT  1 AS one FROM "cars" WHERE LOWER("cars"."reg_no") = LOWER($1) LIMIT $2  [["reg_no", "ขก685"], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.474472 #4] DEBUG -- :   Car Create (1.4ms)  INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["reg_no", "ขก685"], ["group_id", 4], ["created_at", "2018-06-07 15:21:00.472251"], ["updated_at", "2018-06-07 15:21:00.472251"]]
D, [2018-06-07T15:21:00.476690 #4] DEBUG -- :    (1.8ms)  COMMIT

D, [2018-06-07T15:21:00.478864 #4] DEBUG -- :   Group Load (1.7ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."size_id" = $1 AND "groups"."grade_id" = $2 LIMIT $3  [["size_id", 3], ["grade_id", 1], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.480511 #4] DEBUG -- :    (1.0ms)  BEGIN
D, [2018-06-07T15:21:00.482638 #4] DEBUG -- :   Group Load (1.2ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.485111 #4] DEBUG -- :   Car Exists (1.4ms)  SELECT  1 AS one FROM "cars" WHERE LOWER("cars"."reg_no") = LOWER($1) LIMIT $2  [["reg_no", "ขก681"], ["LIMIT", 1]]
D, [2018-06-07T15:21:00.488164 #4] DEBUG -- :   Car Create (1.9ms)  INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["reg_no", "ขก681"], ["group_id", 7], ["created_at", "2018-06-07 15:21:00.485387"], ["updated_at", "2018-06-07 15:21:00.485387"]]
D, [2018-06-07T15:21:00.489452 #4] DEBUG -- :    (1.1ms)  ROLLBACK
rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "cars" violates foreign key constraint "fk_rails_fa6b5abc5a"
DETAIL:  Key (group_id)=(7) is not present in table "sizes".
: INSERT INTO "cars" ("reg_no", "group_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:603:in `async_exec'
...
add_foreign_key
remove_foreign_key
ActiveRecord::Schema.define(version: 2018_06_09_132438) do

  create_table "cars", force: :cascade do |t|
    t.string "reg_no"
    t.integer "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_cars_on_group_id"
    t.index ["reg_no"], name: "index_cars_on_reg_no", unique: true
  end

  create_table "grades", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "groups", force: :cascade do |t|
    t.integer "size_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["grade_id"], name: "index_groups_on_grade_id"
    t.index ["size_id", "grade_id"], name: "index_groups_on_size_id_and_grade_id", unique: true
    t.index ["size_id"], name: "index_groups_on_size_id"
  end

  create_table "prices", force: :cascade do |t|
    t.integer "group_id"
    t.integer "season_id"
    t.integer "amount"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_prices_on_group_id"
    t.index ["season_id"], name: "index_prices_on_season_id"
  end

  create_table "seasons", force: :cascade do |t|
    t.string "name"
    t.integer "start_day"
    t.integer "start_month"
    t.integer "end_day"
    t.integer "end_month"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "sizes", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "name", default: "", null: false
    t.string "email", default: "", null: false
    t.integer "role", null: false
    t.string "encrypted_password", default: "", null: false
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
  end

end