Postgresql v. 14
I want to be able to query all genres related to a movie in a efficient and scalable way, I thought of these approaches and need help deciding, if you know a better approach feel free to post it.
Problem:
I have 2 tables. 1 is movie that contains information related to a movie and the 2nd table is genre in which it contains a list of genres that can be added to movies. Each movie can have 0/null or many genres.
Solution 1:
Create a new table called movie_genre which references movie_id and genre_id. This will need to be maintained by a trigger to keep it updated once a new movie gets inserted.
OR
Solution 2:
Create a new column in movie called genres which contain array of integers that are linked with genre ids, then I'll be able to query the genres like so:
select genre.* from genre join movie on genre.id = any(movie.genres) where movie.id = 1;
Which do you think is a more sensible way of doing it? Would like to hear from you.