PostgreSQL Foreign Keys and References

StandardError: An error has occurred, this and all later migrations canceled:                                               PG::UndefinedTable: ERROR:  relation "departure_airports" does not exist     

: CREATE TABLE "flights" ("id" bigserial primary key, "departure_airport_id" bigint, "arrival_airport_id" bigint, "take_off" timestamp, "duration" time, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_b88a099d1c"

FOREIGN KEY ("departure_airport_id")
REFERENCES "departure_airports" ("id"),
CONSTRAINT "fk_rails_91bb366993"
FOREIGN KEY ("arrival_airport_id")
REFERENCES "arrival_airports" ("id"))
class CreateFlights < ActiveRecord::Migration[5.1] 
def change
create_table :flights do |t|
t.references :departure_airport, foreign_key: true
t.references :arrival_airport, foreign_key: true
t.datetime :take_off
t.time :duration
t.timestamps
end
end
end
add_foreign_key :flights, :airports, column: :departure_airport_id
add_index :flights, :departure_airport_id
class CreateFlights < ActiveRecord::Migration[5.1]
def change
create_table :flights do |t|
t.integer :departure_airport_id
t.integer :arrival_airport_id
t.datetime :take_off
t.time :duration
t.timestamps
end
add_foreign_key :flights,
:airports,
column: :departure_airport_id
add_index :flights, :departure_airport_id
add_foreign_key :flights,
:airports,
column: :arrival_airport_id
add_index :flights, :arrival_airport_id
end
end

--

--

--

Web Developer, Marathon Runner, Coffee Drinker.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andy

Andy

Web Developer, Marathon Runner, Coffee Drinker.

More from Medium

“Hello World” — Welcome to programming

How to Revert Local Changes with Git Restore

Classes and Instances in Object-Oriented Ruby : A Newbie’s Guide

Manage multiple Ruby versions by uru and RVM