What Next?
Well done! That's all the CRUD
operations working :tada:
Still have some time?
⚠️ Please read all the instructions below before proceeding to change your project's code to ensure you understand all requirements.
Further reading
You can read more about them here with some examples: A look at PostgreSQL migration in node
Now you know more about migration... let's go back to our challenge...
The challenge
Can you extend your Music Library project to add a new Album
table and CRUD
operations for it?
-
Start by adding an
Album
table to the database. You can do this by adding another migration file to your repository. -
Your
Album
table will needid
,name
andyear
columns. To keep things simple, theyear
column can be anINT
. -
Albums will also need to be able to reference the artist that released them. Add another column to your table called
artistId
. It should be aforeign key
which references theArtist
table. Have a look on the internet on how you can set a foreign key with PostgreSQL syntax. -
Write a test file called
album-create.test.js
. It will be very similar to theartist-create.test.js
file. -
The test should
POST
to/artists/:id/albums
to create a new album associated to that artist. ⚠️ You will need to make sure there is an artist in your database before you try and create an album. -
The rest of the
CRUD
tests should send their requests to either/albums
or/albums/:id
. Plan your routers accordingly.
Common pitfalls and good things to remember:
-
If you were eager and created the
Album
table by running the migration just with step 2, but haven't set up the foreign key from step 3, you will be tempted to change the second migration you've added. This is not going to work because once a migration occurs, you can't change it. That's because ahash
of what the SQL that was executed is being recorded in themigrations
table. To fix this, you will need toALTER
the table and add a contraint instead - as part of an additional migration. -
Here are some errors you might encounter:
- Hashes don't match.
Solution: This error occurs, most likely, because you've already performed the migration mentioned and you're trying to edit it. Revert the migration to initial SQL command and add a new one with the change you want to make.
Finished? ✅
Fantastic! Let us know on Slack and ask for a code review.