Question:
I am making a small project with a DB of a “book_store”. I am planning to have a two tables :Books -> table(It consists of
id
as a Primary Key, and genre_id
as a Foreign_key of genre).
Genre table will have multiple genres(comedy, horror with its unique id)
Genre -> table(id as a Primary Key, and yes this column will link to Books
table as a primary Key)
Here is the implementation of Book table
Genres
tableBook
table i am getting is like this:
I am using PostgresQLand table of
Genre
Genreenter image description here
So my question is like this , while i am adding a new book ,and putting a genre 2 -> that means comedy, the
Error is occured , that is telling that repeating of Unique key genres_pkey
?
Thanks for feedback!Answer:
One to one means that a Book has a single genre and that a single genre can only be used for a book. You have also specifiedunique=true
, meaning that in the column genre_id
you cannot have the same id twice.Your description is not clear but I’m assuming multiple books can have the same genre.
The mapping in this case changes to a many-to-one:
If you have better answer, please add a comment about this, thank you!