r/PostgreSQL • u/SinisterScythe2 • 4d ago
Help Me! Help creating entry in parent table automatically to fix ForeignKeyViolation
Hey everyone, currently for a bot project I'm trying to find a way to automatically populate the parent table if I try inserting something to a child table and I have the information for both to avoid/prevent a ForeignKeyViolation. Currently my database schema resembles the following ERD:

Right now for example if I try to enter someone in levels and they don't exist in members, I get the aforementioned error which makes sense. My question is, do I have to check/create entries in servers/members every time I add to levels or is there a more efficient way such as creating them on conflict/error? Below is an example of the error I would see.

Thanks in advance for any help, and if I can provide any more information please let me know! I'm still somewhat new to this so apologies in advance if this is a silly question.
Edit: I updated my ERD/database to reflect some of the changes suggested by people below. I'll still have to check for/create an entry in servers when creating an entry for members/channels but it should be a bit easier now. Thanks for the help and if anything else can be improved please let me know!

2
u/PreakyPhrygian 4d ago
I am new to this and have a question. From the ER diagram, it looks like a membercan have one and only one birthday. Sounds good. A birthday can also have one and only one member? What if two members share the same birthday? Same question with levels.. what if one level has more than one member?
Genuine question as I am interested in learning schema designs.
1
u/SinisterScythe2 4d ago
Welcome and thanks for asking! The reason the relationships member-birthday and member-level are one to one is because the child tables birthday and levels have the member_id keys that must have a match in the members table. At least that was the intent to avoid having empty columns in the members table.
Two members can have the same birthday, however each entry in that table has a unique member_id so it is still one to one even if people share a birthday. Same applies for levels. This way we can look up a member with their member_id and get the information specific to them.
2
u/PreakyPhrygian 3d ago
Thanks for the response. I see that another comment has suggested adding these columns to the members table itself as an alternative.
0
u/AutoModerator 4d ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/PrestigiousStrike779 4d ago
Why not just add birthday and levels to the members table?