sqlpostgresqlsql-insertinsert-query

how can I reference another column inside postgres insert query?


I have the following table:

CREATE TABLE tab (
    id SERIAL PRIMARY KEY,
    code TEXT NOT NULL,
    data TEXT
)

In some cases, I'd like to insert a new row ensuring that the code column is generated by the id column. In other cases the code is provided by the user.

For example:

INSERT INTO tab(code, data) VALUES ('code: ' || id::TEXT, 'my data');

The expected result is something like:

id code data
1 code: 1 abc
2 code: 2 xyz

INSERT INTO tab(code, data) VALUES ('user.provided.code', 'my data');

The expected result is something like:

id code data
1 code: 1 abc
2 code: 2 xyz
3 user.provided.code xyz

Is it possibile in one statement?


Solution

  • It sounds like you want to default the coder to something based on the id. Unfortunately, this doesn't work in Postgres:

    create table tab (
        id   integer primary key generated always as identity,
        code text not null default ('code '||id::text),
        data text
    );
    

    One option is a single statement that does both an insert and update:

    with i as (
          insert into tab (code, data)
               values ('', 'my data'
               returning *
         )
    update tab
        set code = 'code: ' || id::TEXT
        where tab.id in (select i.id from i);
    

    Another is to use a trigger that assigns the value.