views:

33

answers:

2

I cannot seem to get this right, I am trying to modify a field to be a foreign key, with cascading delete... what am i doing wrong?

ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field REFERENCES my_foreign_table ON DELETE CASCADE;
A: 

Just guessing: shouldn't you add a foreign key instead of a constraint?

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

Postgresql reference

Anonymous Coward
A foreign key *is* a constraint...
Magnus Hagander
+2  A: 

It would help if you posted the 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;
Magnus Hagander
My bad, but you are right all the same.Cookie for you :)Cheers
Ryan