VB_
VB_

Reputation: 45722

Set subquery result as default value

I want to do the next:

ALTER TABLE bets 
ADD COLUMN owner_id bigint NOT NULL DEFAULT (SELECT min(user_id) FROM users);

I'm confused postgres does not allow to save SELECT min(user_id) FROM users subquery's result to a variable and then reuse it.

Any ideas how to set subquery result as default value?

P.S. bets table is not empty

Upvotes: 0

Views: 2676

Answers (1)

Chris
Chris

Reputation: 40661

This seems to work for me:

create function my_foobar_default(OUT id uuid)
as 'select id from foobar order by X desc limit 1'
language sql

alter table Y
add column if not exists SOMEVALUE uuid not null default my_foobar_default()

Upvotes: 2

Related Questions