Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

using string concatenation to reference a table

$
0
0

For a database systems project which tracks entities histories, I stumbled acros the following problem.

Assume we have the table “entity”

CREATE TABLE entity (
    eID SERIAL PRIMARY KEY,
    eName VARCHAR(255) NOT NULL,
    eValue1 INTEGER,
    eValue2 INTEGER
);

as well as “entity_history”

CREATE TABLE entity_history (
    eID_FK INTEGER REFERENCES entity(eID),
    time TIMESTAMP DEFAULT current_timestamp,
    type VARCHAR(6) CHECK(type IN ('Value1','Value2')),
    val INTEGER,
    importantHistoricalEntryNotInMainTable TEXT
    UNIQUE(eID_FK, time)
);

Usually, the idea is that the history gets an entry on update/insert of the main table.
I have to do it the other way around (due to the entitys history being created based on external website information which is not stored in the main table upon insert), so whenever an entry is added to the history, it should update the current value of the field in the main table.
Thus I tried using

CREATE RULE update_main_table AS ON INSERT TO entity_history
DO
UPDATE entity SET ("e" || NEW.type) = NEW.val;

wwhich yields a syntax error.


Viewing all articles
Browse latest Browse all 1138

Trending Articles