Hi,
Sorry to break into this party 🙂
I have this situation in which I have 3 tables (A B C) related to the same table D, but by definition only one of them could be related to the same D record at a time. So I need to implement a constraint for managing this restriction.
And this is more or less how I would do it in a standard situation.
Table D:
- id_D (PK)
- D_type (CHECK: 'A', 'B', 'C')
- id_A (FK, nullable)
- id_B (FK, nullable)
- id_C(FK, nullable)
-- Constraint: exactly one FK must have a value
CHECK ((id_A IS NOT NULL)::int +
(id_B IS NOT NULL)::int +
(id_C IS NOT NULL)::int = 1)
Has there been any improvements on this direction, that allow to define such thing inside r3 at the relation level?
Otherwise how would you suggest I proceed to fulfill this requirement? Is the backend trigger function still the suggested way to go?
Thank you!!