I want to insert values into a view, which is over multiple tables. I use Postgresql.
My solution is to write a rule which inserts all the data into the right table and adds the foreign keys into the rows.
My question is: Can I somehow declare variables into the rule for recurring select-statements?
The code for the rule is:
create rule insert_new_user as on insert to "collHBRS".loginview do instead(
-- add email_address to email table
insert into "collHBRS".email(email_addr) values (new.login_name);
-- add new empty profile to profile table
insert into "collHBRS".profile(profile_email_fk, profile_address_fk, profile_student_fk, profile_company_fk)
VALUES (
(select email_id from "collHBRS".email where email_addr = new.login_name), -- get email_fk
null,null,null);
-- create new login
insert into "collHBRS".login(login_email_fk, login_password, login_salt, last_login, login_profile_fk)
values (
(select email_id from "collHBRS".email where email_addr = new.login_name), -- get email_fk
new.login_password,new.login_salt,now(),
(select profile_id from "collHBRS".profile where profile_email_fk =
(select email_id from "collHBRS".email where email_addr = new.login_name) -- get profile_fk with email_fk
)
)
);
I repeat the select-statement to get the primary key from my new email entry like 2 times.
(select email_id from "collHBRS".email where email_addr = new.login_name)
I tried it, with DECLARE and WITH AS.
The rule works, it is just not pretty.
Thank you