• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to link my primary key to to another table as a foreign key?

Resolved: How to link my primary key to to another table as a foreign key?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

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
And here is my Genres table
So result of Book table i am getting is like this: I am using PostgresQL
and table of Genre Genre
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_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:
It also seems weird there books cannot have multiple genres (what about horror comedy stories?). In that case you will need a bidirectional one-to-many.

If you have better answer, please add a comment about this, thank you!

java spring-boot spring-data-jpa
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: In a Pinescript v5 Strategy why is my bool not working?

02/04/2023

Resolved: net::ERR_HTTP2_PROTOCOL_ERROR by http get request angular 15.2

02/04/2023

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.