Re: UUID vs serial and currval('sequence_id')

From: Robert Stanford <rstanford(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: UUID vs serial and currval('sequence_id')
Date: 2022-05-02 23:24:24
Message-ID: CAC1FqCFY23SmQJPohSA-qRO=fNR-286fHKsvuMQ-u6NzxFopZw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 3 May 2022 at 08:39, David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
wrote:

> You basically have to use "INSERT ... RETURNING" or variables. Which/how
> depends on the language you are writing in. Pure SQL without client
> involvement requires that you use chained CTEs of INSERT...RETURNING (or I
> suppose you could leverage set_config(), haven't tried that way myself).
> In pl/pgsql you can also use variables, and the same goes for psql - though
> that requires client involvement and so isn't generally that great a choice.
>
>
Thanks, so I can do:

alter table contact add column contactuuid uuid
alter table contactinterests add column contactuuid uuid
alter table contactinterests drop column contactid

with thisuuid as (
SELECT gen_random_uuid() as thisuuid
),
contactuuid as(
INSERT INTO contact(
contactuuid,firstname, lastname)
VALUES(
(select thisuuid from thisuuid ),'John', 'Smith') returning
(select thisuuid from thisuuid )
)
INSERT INTO contactinterests(
contactuuid, interest)
VALUES (
(select thisuuid from contactuuid ),'Fishing')
returning (select thisuuid from contactuuid );

Robert

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2022-05-02 23:27:07 Re: UUID vs serial and currval('sequence_id')
Previous Message Mladen Gogala 2022-05-02 22:41:38 Re: External psql editor