Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:59:11+00:00 2026-06-12T03:59:11+00:00

I have this seven table database storing historically valuable areas(such as old cemetery or

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:59:13+00:00Added an answer on June 12, 2026 at 3:59 am

    There are a bunch of different ways of doing this. My solution uses some controversial features of PostgreSQL so see caveats below.

    CREATE TABLE link_categories (
        id int not null unique,
        label text primary key
    );
    
    INSERT INTO link_categories(id, label)
    VALUES (1, 'Area'), 
           (2, 'Sub-area'),
           (3, 'Inspection'),
           (4, 'Excavation');
    
    CREATE TABLE location_base (
       -- add your base fields here
       link_category int not null,
       refkey bigint not null -- assume all id's on the other tables are bigints?
    );
    
    CREATE TABLE finding_base (
      -- add your base fields here
       link_category int not null,
      refkey bigint not null -- see above
    );
    

    Then I would use table inheritance to create child tables, in order to manage your polymorphic association.

    CREATE TABLE location_area (
       CHECK (link_category = 1),
       FOREIGN KEY (refkey) REFERENCES area(id),
       PRIMARY KEY (...) -- needs to be repeated for each child table
    ) INHERITS (location_base);
    
    CREATE TABLE location_subarea (
       CHECK (link_category = 2),
       FOREIGN KEY (refkey) REFERENCES subarea(id),
       PRIMARY KEY (...) -- needs to be repeated for each child table
    ) INHERITS (location_base);
    

    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:

    • DDL inheritance and pay attention to the caveats!
    • Table partitioning
    • Also read My blog post about table inheritance

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please give me some examples of jump table usage. I have seen this example
I have this table, which can be seen as a basic custom gantt chart:
Suppose I have a string like this: one two three four five six seven
So I have a database with a table, where one of it's entries is
In my scheduling database, I have an assignments table with the following fields: |
I have a database table that essentially contains different types of things. I'll use
I have the following database table consisting of a series of tasks: id BIGINT
I am converting an old dataset into a newly structured database. Currently they have
I'm trying to load data (4 columns from 1 table) from an SQL database
I have written an importer which copies data from a flat table into several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.