Last active
May 25, 2022 09:29
-
-
Save mateuszbialowas/59cb25f204886431777003fa75d269ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Booking < ApplicationRecord | |
belongs_to :room, primary_key: :room_id, foreign_key: :room_id | |
belongs_to :guest, primary_key: :guest_id, foreign_key: :guest_id | |
end | |
class Guest < ApplicationRecord | |
has_many :bookings, primary_key: :booking_id, foreign_key: :booking_id | |
end | |
class Room < ApplicationRecord | |
has_many :bookings, primary_key: :booking_id, foreign_key: :booking_id | |
end | |
class CreateBookings < ActiveRecord::Migration[7.0] | |
def change | |
create_table :bookings, primary_key: :booking_id do |t| | |
t.datetime :start_date, null: false | |
t.datetime :end_date, null: false | |
t.belongs_to :room, foreign_key: true, null: false | |
t.belongs_to :guest, foreign_key: true, null: false | |
end | |
end | |
end | |
class CreateRooms < ActiveRecord::Migration[7.0] | |
def change | |
create_table :rooms, primary_key: :room_id do |t| | |
t.integer :room_number, null: false, default: 1, unique: true | |
t.string :room_type, null: false, default: 'standard_room' | |
end | |
end | |
end | |
class CreateGuests < ActiveRecord::Migration[7.0] | |
def change | |
create_table :guests, primary_key: :guest_id do |t| | |
t.string :guest_name, null: false | |
t.string :guest_passport_no, null: false, unique: true | |
t.string :guest_phone, null: false , unique: true | |
end | |
end | |
end | |
# Error when create booking object | |
Booking.create!( | |
room_id: Room.all.sample.room_id, | |
guest_id: Guest.all.sample.guest_id, | |
start_date: Faker::Date.between(from: Date.today, to: Date.today + 30), | |
end_date: Faker::Date.between(from: Date.today + 30, to: Date.today + 90) | |
) | |
rails aborted! | |
ActiveRecord::StatementInvalid: SQLite3::SQLException: foreign key mismatch - "bookings" referencing "guests" | |
SQLite3::SQLException: foreign key mismatch - "bookings" referencing "guests" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment