Laraue.EfCoreTriggers icon indicating copy to clipboard operation
Laraue.EfCoreTriggers copied to clipboard

add a trigger for updating a specific column

Open Vasypu opened this issue 1 year ago • 2 comments

Is it possible to add to the library support for triggers that are triggered only when certain columns in the table are updated?

expected sql of such a trigger:

CREATE TRIGGER trg_update_trigger
AFTER UPDATE OF column1 ON your_table
FOR EACH ROW
EXECUTE FUNCTION your_update_function();

syntax how it would look like:

modelBuilder.Entity<YourEntity>()
            .AfterUpdateOf(x => x.column1
                .Action(x => x.ExecuteRawSql("some sql")));

Vasypu avatar Dec 27 '23 10:12 Vasypu

You can try something like

modelBuilder.Entity<YourEntity>()
            .AfterUpdate(trigger => trigger
                .Condition(refs => refs.Old.Column1 != refs.New.Column1)
                .ExecuteRawSql("some sql"));

win7user10 avatar Dec 28 '23 14:12 win7user10

that's what I did, but this code generates the following sql

CREATE FUNCTION myFunction() RETURNS trigger as $LC_TRIGGER_AFTER_UPDATE_MYFUNCTION$
BEGIN
    IF NEW."Column1" <> OLD."Column1" OR NEW."Column2" <> OLD."Column2" THEN
    ....
    END IF;
RETURN NEW;
END;
$LC_TRIGGER_AFTER_UPDATE_MYFUNCTION$ LANGUAGE plpgsql;
                       
CREATE TRIGGER LC_TRIGGER_AFTER_UPDATE_MYFUNCTION AFTER UPDATE
ON your_table
FOR EACH ROW EXECUTE PROCEDURE myFunction();

and I need the trigger condition to be triggered not in the trigger body, as it is here.

CREATE TRIGGER trg_update_trigger
AFTER UPDATE OF column1 ON your_table
FOR EACH ROW
EXECUTE FUNCTION your_update_function();

is there any possibility with the next update to add support for triggers on certain table columns?

Vasypu avatar Dec 29 '23 12:12 Vasypu