I'm looking for guidance around a pattern I frequently encounter in my PostgreSQL table design. I want to know if I'm thinking about this wrong or if maybe this is a Relational DB limit.
I have a tables like this
CREATE TABLE feature
(
id serial PRIMARY KEY,
name text NOT NULL,
type text NOT NULL,
);
CREATE TABLE feature_type
(
value text PRIMARY KEY,
comment text
);
ALTER TABLE feature ADD CONSTRAINT
feature_type_fkey FOREIGN KEY (type) REFERENCES feature_type;
INSERT INTO feature_type (value, comment) VALUES
('OBJECT_TRACKING', 'Tracks a basic object.'),
('3D_OBJECT_TRACKING', 'Will track a 3D object'),
('TRIANGLE_TRACKING', 'Track a rectangular object');
I essentially have enforceable types for my feature
table now. My problem is that now I want "settings" for any given feature type. Here is how I currently accomplish this.
CREATE TABLE object_tracking_settings (
id serial PRIMARY KEY,
tracking_radius INT NOT NULL,
);
CREATE TABLE three_d_object_tracking_settings (
id serial PRIMARY KEY,
object_depth_value INT NOT NULL,
);
CREATE TABLE triangle_tracking_settings (
id serial PRIMARY KEY,
corner_comparison INT NOT NULL,
);
ALTER TABLE feature
ADD COLUMN object_tracking_settings_id uuid NOT NULL,
ADD COLUMN three_d_object_tracking_settings_id uuid NOT NULL,
ADD COLUMN triangle_tracking_settings_id uuid NOT NULL;
ALTER TABLE feature ADD CONSTRAINT
object_tracking_settings_fkey FOREIGN KEY (object_tracking_settings_id) REFERENCES object_tracking_settings;
ALTER TABLE feature ADD CONSTRAINT
three_d_object_tracking_settings_fkey FOREIGN KEY (three_d_object_tracking_settings_id) REFERENCES three_d_object_tracking_settings;
ALTER TABLE feature ADD CONSTRAINT
triangle_tracking_settings_fkey FOREIGN KEY (triangle_tracking_settings_id) REFERENCES triangle_tracking_settings;
Now my client side applications can get all the feature
's, switch on the type and obtain the appropriate settings. Is there a better way to do this? I guess I could call this "polymorphism" in a relational DB. Should I just be breaking each feature out into it's own table?