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
idas a Primary Key, and
genre_idas 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
Bookstable as a primary Key) Here is the implementation of
Booktable i am getting is like this: I am using PostgresQL
and table of
enter 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 specified
unique=true, meaning that in the column
genre_idyou 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!