Only one of those fk constraints works per column of the same instance of the table. You would have to add one column for each fk. Or have two documentstore
tables.
As you clarified, the same row in documentstore
belongs to either a letter or an email, but only to a single one of those, while each letter / email can have multiple documents.
Hence my new advice: stick with table design you have now, but create two separate tables. There is no gain in having them in the same table. The fact that both tables share the same structure is no good reason to share the data.
You can have schema_a.documentstore
and schema_b.documentstore
inheriting from master.documentstore
. That would mainly be useful if you have use cases dealing with all rows in both tables at once. Be sure to read the chapter about limitations of inheritance in Postgres. In particular, it won't allow you to define a single fk constraint:
A serious limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single tables, not to their inheritance children. This is true on both the referencing and referenced sides of a foreign key constraint.
Related answers with code examples:
Find out which schema based on table values
Create a table of two types in PostgreSQL