Search Postgresql Archives

Re: [EXTERNAL]: Re: UPSERT in Postgres

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/6/23 18:27, Louis Tian wrote:
Hi Adrian,

Thank you. I think this is a better approach than trigger-based solution, at least for my taste. That being said, it does require some logic to push to the client side (figuring out which required column value is missing and set it value to the existing one via reference of the table name). Still wish there would be UPSERT statement that can handle this and make dev experience better.

Another way to make the experience easier:

alter table person alter COLUMN name set default 'cat';

 \d person
                  Table "public.person"
  Column   |  Type   | Collation | Nullable |   Default
-----------+---------+-----------+----------+-------------
 id        | integer |           | not null |
 name      | text    |           | not null | 'cat'::text
 is_active | boolean |           |          |

select * from person;
 id | name | is_active
----+------+-----------
  0 | foo  | NULL


insert into person(id, is_active) values (0, true) on conflict ("id") do update set (id, is_active) = (excluded.id, excluded.is_active);
INSERT 0 1

select * from person;
 id | name | is_active
----+------+-----------
  0 | foo  | t



Cheers,
Louis Tian

--
Adrian Klaver
adrian.klaver@xxxxxxxxxxx






[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux