2

i have a "before update trigger" on a table. How to set default value on a column ? :

CREATE OR REPLACE FUNCTION trigger() RETURNS TRIGGER AS
$$

TD["new"]["test"] = DEFAULT # Doesn't work

$$ LANGUAGE plpython2u VOLATILE;

2 Answers2

0

From the documentation:

... you can return None or "OK" from the Python function to indicate the row is unmodified, "SKIP" to abort the event, or [...] "MODIFY" to indicate you've modified the new row.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Nick Barnes
  • 19,816
  • 3
  • 51
  • 63
  • ... but that is not the default value. – Laurenz Albe Sep 05 '18 at 09:53
  • 1
    @LaurenzAlbe: You say "**the** default value", but I read the question as "How to set **a** default value" (and took `DEFAULT` to be some pseudocode placeholder, figuring that it "doesn't work" because there's no return statement). On closer inspection, it doesn't actually say either. I feel like we need some clarification... – Nick Barnes Sep 05 '18 at 10:44
0

I don't think that there is a way to do that, as default values are evaluated before the triggers are called.

For simple default values (constants), you may be able to work around that by looking up pg_get_expr(adbin, adrelid) AS default_value in the corresponding entry in pg_catalog.pg_attrdef and using that, but it sure ain't a pretty solution.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263