postgresqlforeign-keysdefault

How to change a default setting temporarily in PostgreSQL?


In my psql script all foreign key references include the clause on delete cascade on update cascade, because the default is on delete no action on update no action:

create table x (
  y bigint
    references schema1.table1(column1) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE
);

This construct is repeated hundreds of times.

So I would rather like to write:

create table x (
  y bigint 
    references schema1.table1(column1)
);

How to change the default in Postgres temporarily?


Solution

  • No, you will have to repeat the text every time.

    I understand your desire; I often wish that NOT NULL were the default for columns. But such a feature would be dangerous. Any configuration that alters the semantics of SQL is problematic, because it leads to confusion. Imagine there were such a setting, and you posted your SQL code in — say — a Stackoverflow question: we'd always have to think of clarifying how you configured the database before we could even understand your code.

    You don't have to write your table definitions every day. Use a good text editor and define a macro.