Using PostgreSQL, I need to automatically set a field value in an inserted row based on the output of a function using another field from the very same row that is being inserted. For example, given a table:
CREATE TABLE my_table
(
id UUID DEFAULT uuid_generate_v4(),
code VARCHAR(50) PRIMARY KEY,
value VARCHAR(50),
last_updated TIMESTAMP WITH TIME ZONE DEFAULT now()
)
When inserting a new row e.g.
INSERT INTO my_table (code, value)
VALUES ('a','a'),
('b','b');
I need the field my_table.id to be automatically set for those two new rows based on the output of this parametrized function based on value:
CREATE FUNCTION gen_deterministic_id(val varchar)
RETURNS TRIGGER AS
$$
BEGIN
NEW.id = md5('xx' || val)::uuid;
RETURN NEW;
END;
$$ language 'plpgsql';
NOTE: I really need the function to be parametrizable (to be able to pass any parameter).
The pseudocode would be something like this:
for each new row:
row.id = gen_deterministic_id(row.value)
then insert_to_table
Do you guys know if this is possible to implement using e.g. triggers or the like in PostgreSQL?