I have this seven table database storing historically valuable areas(such as old cemetery or housing sites from stone ages), that can be divided into sub-areas. There is inspections and excavations done to these areas. All of the four tables, area, sub-area, inspection and excavation, can have one or more “location” or “finding”.
Location- and finding-tables are at the moment related to link-table, that contains the name of target table and target id in that table, such as target_table="subarea", target_id=5.
The problem is that storing table names in database is to my understanding not good practice. So what would be optimal solution to link location and finding with N-1 relation to any of the four tables?
There are a bunch of different ways of doing this. My solution uses some controversial features of PostgreSQL so see caveats below.
Then I would use table inheritance to create child tables, in order to manage your polymorphic association.
etc. Repeat for findings tables
Note that table inheritance is a somewhat controversial feature on PostgreSQL. You need to think both in terms of utilizing some of the underlying quirks of table inheritance to your advantage here and also implement it like a table partitioning system. Note that in this case link_category becomes essentially part of your primary key.
Before you go with this solution however, please read the PostgreSQL docs on:
Note that this is a somewhat dangerous feature but polymorphic associations generally pose dangers. I am recommending this specifically here because I think that it simplifies your failure cases even though it does so at the cost of some other things.