PG :: ForeignKeyViolation:ERROR:on rake db:reset

时间:2016-09-02 10:43:43

标签: sql ruby-on-rails database ruby-on-rails-4 rake

执行rake db:reset时出现以下错误:

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "routes" violates foreign key constraint "fk_rails_0ed30cb49b"
DETAIL:  Key (planification_id)=(0) is not present in table "planifications".

错误详情:

: INSERT INTO "routes" ("evite_autoroute", "places_disponibles", "duration", "distance", "price", "date_heure_aller", "mode_reservation", "modele", "taille_bagages", "detours", "flexibilite", "event_name", "event", "confort", "ladies_only", "men_only", "description_aller", "planification_id", "vehicule_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22) RETURNING "id"
/home/charles/cardating/web/db/seeds.rb:71:in `<top (required)>'

我首先认为它是Route和Planification之间缺少的外键,所以我在迁移文件中添加了add_index :planifications, :route_id我们创建的Planification表,但它没有改变任何内容。

创建Planification迁移文件:

class CreatePlanifications < ActiveRecord::Migration
  def change
    create_table :planifications do |t|
      t.string :modele
      t.text   :jours

      t.belongs_to :route, index: true

      t.timestamps null: false
    end

    add_index :planifications, :route_id
  end
end

创建路线迁移文件:

class CreateRoutes < ActiveRecord::Migration
  def change
    create_table :routes do |t|
      t.boolean :evite_autoroute
      t.integer :places_disponibles
      t.integer :distance
      t.integer :duration
      t.decimal :prix, precision: 3, scale: 2
      t.datetime :date_heure_aller
      t.datetime :date_heure_retour
      t.date :date_du
      t.date :date_au
      t.string :mode_reservation
      t.string :modele
      t.string :taille_bagages
      t.string :detours
      t.string :flexibilite
      t.string :event_name
      t.integer :delais_reponse
      t.boolean :event
      t.boolean :confort
      t.boolean :ladies_only
      t.boolean :men_only
      t.text :description_aller
      t.text :description_retour
      t.references :planification, index: true, foreign_key: true
      t.references :vehicule, index: true, foreign_key: true

      t.belongs_to :user, index: true

      t.timestamps null: false
    end
  end
end

0 个答案:

没有答案