I see that most people just hate having a circular dependency in the database design. And since the support for this is “tricky” in most database engines, I was wondering if there’s a way around this design:
I have a users table and a pictures table
Every picture has an userId (the user who inserted it)
Every user has a profile picture
I might just create a ProfilePictures table, but it would cause issues in some other places (like picture comments).
I’m aware that there are some other questions related to this issue, but they’re more related to partent-child relationships, which is not the case here.
So, is it ok to use a circular dependency here? or if not, how would you avoid it?
Without circular references between tables:
The only difference with this design and your needs is that a user may not have a profile picture associated with him.
With circular references between tables:
The
profilepictureidcan be set toNOT NULLbut then you have to deal with the chicken-and-egg problem when you want to insert into the two tables. This can be solved – in some DBMS, like PostgreSQL and Oracle – using deferred constraints.