Single Table or Multi-Table Inheritance for Thing Model #12
ksmuczynski
started this conversation in
ADR
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
status: Accepted
date: 2025-7-29
ADR: Single Table or Multi-Table Inheritance for Thing Model
Context and Problem Statement
The Bureau is developing a new database schema to more effectively store data associated with hydrogeologic monitoring stations or
Things
. A Thing is a generic entity with common properties (name, date installed). There also exist specific, mutually exclusive subtypes of stations, such as Wells and Precipitation Stations. Each subtype has its own unique set of attributes that do not overlap.total depth
and ameasuring point height
orifice diameter
andmaterial
The core architectural problem is to model this generic-parent-to-unique-child relationship in a way that:
The system must be able to efficiently query for all Things, as well as filter and retrieve Things of a specific type with their unique attributes. We anticipate adding more Thing types in the future (e.g., Spring, Soil), each with its own set of unique fields. We need a database schema design that accommodates this inheritance-like data model cleanly and efficiently, and can also apply to other similar instances of parent-child related tables (e.g. Sample, Observation).
Decision Drivers
Considered Options
Decision Outcome
Chosen Option: Single-Table Inheritance (STI).
We will implement a single Thing table. This decision is based on the following rationale:
thing_type
values. This avoids the complexity of managing multiple tables and JOINs inherent in MTI.While we acknowledge the drawback of having potentially many NULL values, the data integrity benefit of guaranteeing that a Thing can only be one subtype, along with the performance benefits for our most common query patterns and the overall simplicity of the model, outweigh this concern for our current scale and foreseeable future. The JSON field approach was deemed too flexible, sacrificing the data integrity and queryability that a structured relational model provides. The MTI approach failed to ensure that a Thing can only be one subtype.
Consequences
Positive
Negative
casing diameter
must be non-null for Well but must be null for Precipitation) at the database level without complex check constraints. This logic must be enforced by the application.Pros and Cons of the Options
1. Multi-Table Inheritance (MTI)
Pros:
Cons:
2. Single-Table Inheritance (STI)
Pros:
Cons:
3. JSON Field
Pros:
Cons:
Beta Was this translation helpful? Give feedback.
All reactions