Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relational Design: Column Attributes

I have a system that allows a person to select a form type that they want to fill out from a drop down box. From this, the rest of the fields for that particular form are shown, the user fills them out, and submits the entry.

Form Table:

| form_id | age_enabled | profession_enabled | salary_enabled | name_enabled |

This describes the metadata of a form so the system will know how to draw it. So each _enabled column is a boolean true if the form should include a field to be filled out for this column.

Entry Table:

| entry_id | form_id | age | profession | salary | name | country |

This stores a submitted form. Where age, profession, etc stores the actual value filled out in the form (or null if it didn't exist in the form)

Users can add new forms to the system on the fly.

Now the main question: I would like to add the ability for a user designing a new form to be able to include a list of possible values for an attribute (e.g. profession is a drop down list of say 20 professions instead of just a text box when filling out the form). I can't simply store a global list of possible values for each column because each form will have a different list of values to pick from.

The only solution I can come up with is to include another set of columns in Form table like profession_values and then store the values in a character delimited format. I am concerned that a column may one day have a large number of possible values and this column will get out of control.

Note that new columns can be added later to Form if necessary (and thus Entry in turn), but 90% of forms have the same base set of columns, so I think this design is better than an EAV design. Thoughts?

I have never seen a relational design for such a system (as a whole) and I can't seem to figure out a decent way to do this.

like image 607
Davis Dimitriov Avatar asked Feb 23 '23 17:02

Davis Dimitriov


1 Answers

Create a new table to contain groups of values:

CREATE TABLE values (
    id SERIAL,
    group INT NOT NULL,
    value TEXT NOT NULL,
    label TEXT NOT NULL,
    PRIMARY KEY (id),
    UNIQUE (group, value)
);

For example:

INSERT INTO values (group, value, label) VALUES (1, 'NY', 'New York');
INSERT INTO values (group, value, label) VALUES (1, 'CA', 'California');
INSERT INTO values (group, value, label) VALUES (1, 'FL', 'Florida');

So, group 1 contains three possible values for your drop-down selector. Then, your form table can reference what group a particular column uses.

Note also that you should add fields to a form via rows, not columns. I.e., your app shouldn't be adjusting the schema when you add new forms, it should only create new rows. So, make each field its own row:

CREATE TABLE form (
    id SERIAL,
    name TEXT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE form_fields (
    id SERIAL,
    form_id INT NOT NULL REFERENCES form(id),
    field_label TEXT NOT NULL,
    field_type INT NOT NULL,
    field_select INT REFERENCES values(id),
    PRIMARY KEY (id)
);

INSERT INTO form (name) VALUES ('new form');
$id = last_insert_id()
INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'age', 'text');
INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'profession', 'text');
INSERT INTO form_fields (form_id, field_label, field_type) VALUES ($id, 'salary', 'text');
INSERT INTO form_fields (form_id, field_label, field_type, field_select) VALUES ($id, 'state', 'select', 1);
like image 98
Alex Howansky Avatar answered Mar 03 '23 17:03

Alex Howansky