Postgresql limitation

I can’t understand that this is correct, I am trying to change the field to be a foreign key, with cascading deletion ... what am I doing wrong?

ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field REFERENCES my_foreign_table ON DELETE CASCADE; 
+41
postgresql foreign-keys
Jun 09 '10 at 11:12
source share
4 answers

This will help if you post an error message. But I think you are just missing the parenthesis:

 ALTER TABLE my_table ADD CONSTRAINT my_fk FOREIGN KEY (my_field) REFERENCES my_foreign_table ON DELETE CASCADE; 
+76
Jun 09 '10 at 12:02 on
source share

Just guess: should you add a foreign key instead of a restriction?

 ALTER TABLE my_table ADD FOREIGN KEY (my_field) REFERENCES my_foreign_table; 

Postgresql Link

+12
Jun 09 '10 at 11:21
source share

I still somehow missed the answer with an explicitly specified foreign column ( foreign_field ):

 ALTER TABLE my_table ADD CONSTRAINT my_fk FOREIGN KEY (my_field) REFERENCES my_foreign_table (foreign_field) ON DELETE CASCADE; 
+12
Mar 26 '16 at 17:35
source share

This works for me, I add a column to the table, and then add a constraint with links to another table:

 -- add column to table ALTER TABLE schema.table ADD COLUMN my_column type; -- add constraint to column ALTER TABLE schema.table ADD CONSTRAINT fk_name FOREIGN KEY (column) REFERENCES schema.table (column) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION; 
+1
Apr 25 '17 at 14:58
source share



All Articles