Reputation: 45722
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
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