Skip to content

Trigger on Insert OR Update to configure a timestamp #119

@sdukehart-omnesoft

Description

@sdukehart-omnesoft

I feel like I'm a bit dense here.

I haven't been able to get something like https://www.npgsql.org/efcore/modeling/generated-properties.html#timestamp-generation working:

CREATE FUNCTION "Blogs_Update_Timestamp_Function"() RETURNS TRIGGER LANGUAGE PLPGSQL AS $$
BEGIN
    NEW.timestamp := CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$;

CREATE TRIGGER "UpdateTimestamp"
    BEFORE INSERT OR UPDATE
    ON "Blogs"
    FOR EACH ROW
    EXECUTE FUNCTION "Blogs_Update_Timestamp_Function"();

I end up having to do something like:

        entity.BeforeInsert(trigger =>
            trigger.Action(action => action.ExecuteRawSql("NEW.timestamp = CURRENT_TIMESTAMP"))
        );
        entity.BeforeUpdate(trigger =>
            trigger.Action(action => action.ExecuteRawSql("NEW.timestamp = CURRENT_TIMESTAMP"))
        );

Ideally, I'd do something like:

        // Note BeforeInsertOrUpdate, which doesn't currently exist
        entity.BeforeInsertOrUpdate(trigger =>
            trigger.Action(action =>
                // Some NON "ExecuteRawSql" action here against my entity type
            )
        );

But I can't figure out:

  1. How to do this without ExecuteRawSql
  2. How to do something like a combined BeforeInsertOrUpdate (less of a concern as we can have a default value on the column)

Thanks for any insights.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions