0

I have a table names Locations. It has columns [ID, GUID_ID, NAME, GEOMETRY]. I want to update GUID_ID column after a row inserted. So I will create a trigger to do this.

CREATE TRIGGER update_id 
   AFTER INSERT ON Locations
   ?? (How can Update GUID_ID column as uuid_generate_v4())

I set a default value for this column. But third party applications like QGIS inserts thousands of geometry records at same time. So the default value does not fill all columns. They are null. So I need trigger for solution.

1 Answer 1

1

I think it dosnt matter how much records QGIS enters, if you have defined a default value for field. May be QGIS is entering empty space instead. You can also add "NOT NULL" constraint in the field definition. However trigger can be used like this. I am setting the GUID_ID value BEFORE INSERTING though.

CREATE TRIGGER update_id 
   BEFORE INSERT ON Locations
   FOR EACH ROW EXECUTE PROCEDURE fn_trgr();

CREATE OR REPLACE FUNCTION fn_trgr() RETURNS TRIGGER AS $$
BEGIN
    IF pg_trigger_depth() <> 1 THEN
        RETURN NEW;
    END IF;
    NEW.GUID_ID = uuid_generate_v4(); -- or whatever value you wants to set
    RETURN NEW;

END;
$$ LANGUAGE plpgsql;
Sign up to request clarification or add additional context in comments.

6 Comments

I will update only inserted row. I think qgis sends as null GUID_ID field.
Then just add a "NOT NULL" constraint in column of table DDL
How can I check if GUID_ID column exist?
from the definition of your table Locations
Does matter guid id uppercase or lowercase?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.