have two tables as following Post table and Commen...
# announcements
r
have two tables as following Post table and Comment table I need to create another table called Activity table which will store aggregate values of likes performed on post or comment. I want to use activity id column which must be EITHER post id or comment id. How do i do enforce this condition in database? (preferably hibernate)
d
I think the simplest way is to have two columns, one for post, one for comment. And either can be null. Then you can use a
CHECK
to enforce that exactly one of them is not null
v
Why must them be in one table, not in two? And can ids be the same?
r
@Vampire its a table that keeps likes performed on post or comment. so i need to store either post or comment id as activity id. id could be same, but i can use another column called type. so its not a problem due to composite primary key. problem is, i want to use either post id or comment id as activity id.
@diesieben07 this could be good, but i was looking for single colum solution if possible
d
you can't have a foreign key on two tables, so its impossible to do properly with a single column.
r
if thats the thing, i'll go with your solution. thanks
v
In SQL it is not possible, with Hibernate it should work afair.
With a discriminator column as you already mentioned
d
Anyone who needs to work with your DB outside of hibernate will hate you,if you do that! 😄
👍 1
v
Yeah, but well, sometimes this is wanted. 😄