У меня есть 2 таблицы для моего приложения для бронирования авиабилетов на рельсы - одна для полета и одна для аэропортов. Я пытаюсь накачать данные о рейсах и аэропортах с помощью заполнения и продолжаю получать ошибку ограничения внешнего ключа при заполнении данных полета ниже (rake db: seed). Я попытался поиграть с ассоциациями моделей, но не совсем уверен, является ли это источником проблемы.
Таблицы
Flight(id: integer, start_airport_id: integer, end_airport_id: integer, departure_time: datetime, flight_duration: integer, created_at: datetime, updated_at: datetime)
Airport(id: integer, airport_code: string, created_at: datetime, updated_at: datetime)
Исходный файл
Airport.delete_all
airports = Airport.create([{ airport_code: 'SFO' }, { airport_code: 'NYC' }, { airport_code: 'LAX' },
{ airport_code: 'LAS' }, { airport_code: 'DEN' }, { airport_code: 'SEA' }, {airport_code: 'PHX' }])
Flight.delete_all
flights = Flight.create([{ start_airport_id: 1, end_airport_id: 2, departure_time: DateTime.new(2020, 8, 29, 16, 30, 0), flight_duration: 5 },
{ start_airport_id: 1, end_airport_id: 3, departure_time: DateTime.new(2020, 7, 13, 13, 0, 0), flight_duration: 2 },
{ start_airport_id: 1, end_airport_id: 4, departure_time: DateTime.new(2020, 9, 7, 9, 30, 0), flight_duration: 2 },
{ start_airport_id: 2, end_airport_id: 7, departure_time: DateTime.new(2020, 10, 6, 10, 0, 0), flight_duration: 5 },
{ start_airport_id: 1, end_airport_id: 2, departure_time: DateTime.new(2020, 12, 4, 6, 30, 0), flight_duration: 6 },
{ start_airport_id: 3, end_airport_id: 2, departure_time: DateTime.new(2020, 11, 4, 11, 0, 0), flight_duration: 6 }])
Модели
class Airport < ApplicationRecord
has_many :departing_flights, class_name: "Flight"
has_many :arriving_flights, class_name: "Flight"
end
class Flight < ApplicationRecord
has_many :to_airport, foreign_key: :end_airport_id, class_name: "Airport"
has_many :from_airport, foreign_key: :start_airport_id, class_name: "Airport"
end