PG :: ForeignKeyViolation:错误:在表格上插入或更新" ..."违反外键约束

时间:2018-06-07 13:21:05

标签: ruby-on-rails postgresql pg

Ruby on rails

以下是开发mySQL

但是在Production PG上提出错误(部署到Heroku,postgreSQL),只有一些记录!

我有什么:

  • 汽车尺寸(S,M,L),例如
  • 汽车等级(新的,普通的,旧的),例如
  • 尺寸等级制作差异汽车组(使用多次透过),此示例中包含18个组。
  • 然后汽车属于差异群组

当我尝试触发下面的命令时,可以正常工作6次

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

但是

Car.create(reg_no: "007", group_id: 7)

和其他人最终:

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之后的所有group_id。我不明白是什么让他们与众不同。

Group_id 7由Size:M和Grade: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">

第6组的同一组请求不会引发错误

irb(main):005:0> Group.find(6)
D, [2018-06-07T15:00:27.431814 #4] DEBUG -- :   Group Load (1.5ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
=> #<Group id: 6, size_id: 2, grade_id: 3, created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">
irb(main):006:0> Size.find(2)
D, [2018-06-07T15:00:40.946858 #4] DEBUG -- :   Size Load (1.4ms)  SELECT  "sizes".* FROM "sizes" WHERE "sizes"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
=> #<Size id: 2, name: "SM", created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">
irb(main):007:0> Grade.find(3)
D, [2018-06-07T15:00:49.220472 #4] DEBUG -- :   Grade Load (1.4ms)  SELECT  "grades".* FROM "grades" WHERE "grades"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
=> #<Grade id: 3, name: "Old", created_at: "2018-06-06 18:07:26", updated_at: "2018-06-06 18:07:26">

模型

class Car < ApplicationRecord
  belongs_to :group
  validates :reg_no, presence: true, length: { maximum: 6 },
                     uniqueness: { case_sensitive: false }
end

class Group < ApplicationRecord
  belongs_to :size
  belongs_to :grade
  has_many :cars, dependent: :destroy
  validates :size_id, presence: true
  validates :grade_id, presence: true
end

class Size < ApplicationRecord
  has_many :groups, dependent: :destroy
  has_many :grades, through: :groups
  validates :name, uniqueness: true
end

class Grade < ApplicationRecord
  has_many :groups, dependent: :destroy
  has_many :sizes, through: :groups
end

SCHEMA

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'
...

2 个答案:

答案 0 :(得分:0)

我在开发中切换到PG,因此它会产生相同的错误。

我找不到如何修复当前架构。可能是因为在开发期间我创建了表和列,然后更改了列和表的名称。即使最终架构看起来很好,但可能在某些阶段外键未正确更改。当我检查我的迁移时,对我的唯一怀疑是rename_column,即使这种方法也改变了相应的索引。

因此,我编写了具有所有必需详细信息的新迁移,甚至它生成的架构与此处列出的原始架构完全相同,但它有所帮助。

在搜索过程中找到有趣的迁移方法:

add_foreign_key
remove_foreign_key

但是id并没有帮助。

新生成的架构:

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

答案 1 :(得分:0)

我们遇到了同样的问题。

您需要检查一下schema.rb并查看按钮。

您将看到使用foreign_key创建的迁移,您必须再次对其进行更新并运行迁移。