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

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: UUID vs serial and currval('sequence_id')
Дата
Msg-id CAKFQuwYD1niYAB1RRAMEJUrePqXxQ1tzgFnFdOjQ-xpLhqpzzg@mail.gmail.com
обсуждение исходный текст
Ответ на UUID vs serial and currval('sequence_id')  (Robert Stanford <rstanford@gmail.com>)
Ответы Re: UUID vs serial and currval('sequence_id')  (Robert Stanford <rstanford@gmail.com>)
Список pgsql-general
On Mon, May 2, 2022 at 3:33 PM Robert Stanford <rstanford@gmail.com> wrote:

--insert statement as single transaction returning contactid
INSERT INTO contact(
    firstname, lastname)
  VALUES('John', 'Smith');
INSERT INTO contactinterests(
    contactid, interest)
  VALUES (currval('contact_contactid_seq'),'Fishing') 
returning currval('contact_contactid_seq');

Which is very nice as it gives us back the contactid.

Is it possible to get similar functionality using gen_random_uuid() or uuid-ossp?


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.

David J.

В списке pgsql-general по дате отправления:

Предыдущее
От: Robert Stanford
Дата:
Сообщение: UUID vs serial and currval('sequence_id')
Следующее
От: Mladen Gogala
Дата:
Сообщение: Re: External psql editor