Created
February 25, 2019 21:42
-
-
Save twopoint718/bd6e0042ca3e5586904606f0bb46192a to your computer and use it in GitHub Desktop.
Bidirectional foreign key constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table address ( | |
address_id serial primary key, | |
person_id integer | |
); | |
create table person ( | |
person_id serial primary key, | |
address_id integer | |
); | |
alter table address | |
add constraint fk_address_person_id | |
foreign key (person_id) references person (person_id) | |
on delete cascade | |
deferrable initially deferred; | |
alter table person | |
add constraint fk_person_address_id | |
foreign key (address_id) references address (address_id) | |
on delete cascade | |
deferrable initially deferred; | |
begin; | |
-- set constraints all deferred; | |
insert into person (person_id, address_id) values (1, 1); | |
insert into address (address_id, person_id) values (1, 1); | |
commit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment