PG :: ForeignKeyViolation: ОШИБКА: вставка или обновление таблицы "..." нарушает ограничение внешнего ключа - PullRequest
0 голосов
/ 07 июня 2018

Ruby на рельсах

Следующее работает над разработкой 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".

Таким образом, все group_id после 6 не принимаются.Я не понимаю, что отличает их.

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

СХЕМА

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

1 Ответ

0 голосов
/ 09 июня 2018

Я переключился на PG в разработке, поэтому он генерирует ту же ошибку.

Не могу найти как исправить текущую схему.Может быть, потому, что в процессе разработки я создавал таблицы и столбцы, а затем менял имена столбцов и таблиц.Даже окончательная схема выглядит нормально, но, возможно, на каком-то этапе внешний ключ не был изменен должным образом.Когда я проверял свою миграцию, единственным подозрением для меня было rename_column, хотя этот метод также изменял соответствующий индекс.

Так что я написал новые миграции со всеми необходимыми деталями, что и даже генерирует схему, идентичную оригинальной, перечисленной здесь, ноэто помогло.

Интересные методы миграции, которые были найдены при поиске:

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