So we need to set up some foreign keys in our database and we'll do the reservations table first because
it just has one.
It has a foreign key from this column remedy linking to the ID column in the room table.
So how do we do that?
And fortunately, SOTA makes this remarkably easy.
So I'm going to go to my terminal and I'm going to write SOTA, generate FIDs, create Akay for foreign
key for reservations table and it creates my migration's.
So let's open one up, we'll open up the newest one, which is this one right here to make sure it's
the up migration.
And I'm just going to paste in the actual migration because it's a little tricky to type and I don't
want to make mistakes.
So the syntax is add, underscore for and underscore key that tells SOTA we're going to create a foreign
foreign key, then an opening parentheses that in quotes the name of the table that's going to hold
the foreign key, that's reservations in this case.
Then the name of the column, that is the foreign key and that's called room ID in our reservations
table.
And that links to in these curly brackets right here on the room's table in square brackets with quotation
marks around it, the name of the column that remedy should link to.
So it says on the table reservations for the column room ID create a foreign key that points to the
rooms table and the ID column.
And then what do you want this foreign key to do?
Well, to start with, when someone deletes the that corresponding entry in the room table, I'm just
going to say Kaskade also delete this reservation when someone updates the foreign key.
If it used to be ID one and somebody changes it to 82 on the table rooms, then again, update the foreign
key here, just Kaskade those changes.
So let's try broadening that migration and see what happens.
So sota migrate and there goes.
We now have a foreign key relationship and I can go over to my I'm going to use post to go again and
look at this so I can look at the reservations table and there it is, there's the description of it.
So what is this actually happened.
So I can actually look at where is it here, reservations.
I don't want to look at the structure.
I want to look at the DDL, the data description, language, and it actually says here that I have
my reservations and I have room ID.
It references rooms, ID undelete Kaskade on update Kaskade.
So my foreign key is now there.
Now how do I get rid of a foreign key.
I need to write a down migration.
Well, the syntax for that's actually pretty simple, let's go back over to our terminal and open our
down migration for the foreign chief reservations and just right.
Drop foreign Haidian numbers.
Better for our Aegean key at the table.
Name is reservations.
That's where the foreign he lives and the foreign key name.
Now, that is not as simple as you might think.
The foreign keys actually named by postgrads.
And it uses a particular syntax for doing that.
And it's pretty simple.
So I want to go to my postgrads.
I want to go to bookings.
I want to look at my schema.
Tables.
Reservations, foreign chiefs, and there it is, it is called reservations, underscore rooms, underscore
idy, underscore coffeecake.
So let's go back in their reservations underscore and what was it?
Underscore rooms, underscore idee, underscore EPK reservation rooms, IDFC of migrate down and there
it is.
So it deletes it and I can put it back in so to migrate and it works great.
So did that flunkey.
Let's add the rest of them.
Where else do we need foreign keys.
Well let's do that in the next lecture.
I think this one's long enough.