Is it possible to add a custom made function in th...
# help
m
Is it possible to add a custom made function in the default value of a column field? For instance, what I expect to happen is that I insert a row without any value for the column in question. Because there is no value there, it runs that function and ends up with a value that is dependent on lets say the last added row.
n
Hello @Muezz! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
n
wubba_lubba_whatever (2022-04-07)
m
oh. Any idea if this will ever be added?
f
if u are going to use parameters on the function to decide the default value the best option rn is to use a table tigger
here u have an example
Copy code
pgsql
create table the_table (
    trade_id int not null,
    group_id int);

create or replace function trade_id_trigger ()
returns trigger language plpgsql as $$
begin
    new.group_id:= new.trade_id+ 1;
    return new;
end $$;

create trigger trade_id_trigger
before insert or update on the_table
for each row execute procedure trade_id_trigger();

insert into the_table values (1,1);

select * from the_table;
as u see here
new.group_id:= new.trade_id+ 1;
u can use the values that u are inserting on the table to decide the default value for the empty column
m
I need to add one value from the new row and one value from last row. As this is written in sql, I am pretty sure I could run a query to find the last added row and get the relevant value.
f
yes in the trigger you could execute whatever is needed to set that value you need...
m
okay great. Thanks for the help
Hey, I was looking into triggers like you suggested and came across RPCs or Stored Procedures. Are these the same thing as triggers?