What is the best way in Postgres to inherit values from parent row recursively if not provided?
0
votes
1
answer
365
views
Is there a pattern in SQL where a child row inherits empty (null?) values from a parent?
E.g., given the following 'chickens' table (sorry):
| id | parent_id | name | flys | noise | weight | egg_color | special_feathers |
|----|-----------|----------------|-------|---------|--------|-----------|------------------|
| 1 | | chicken | true | clucks | 5lbs | white | false |
| 2 | 1 | maran | | | | brown | |
| 3 | 1 | silkie | false | squeeks | 4lbs | | |
| 4 | 3 | silkie frizzle | | | | | true |
I want to easily be able to do:
select * from chickens where egg_color = 'white'
and have it return all the chickens except for the maran.
I don't want to explicitly set the values in child rows because I want to be able to update the parent data and have it updated everywhere.
I think this can be accomplished via a recursive view, however I am curious if there are any other solutions.
I am also not sure how to handle explicitly empty states other than with an empty string (which won't work for non-string fields). This might not be something we need to accommodate but want to see if anyone has ideas.
Asked by Sabrina Leggett
(101 rep)
Mar 21, 2024, 02:23 AM
Last activity: Mar 23, 2024, 05:22 AM
Last activity: Mar 23, 2024, 05:22 AM