I have the following set up,
CREATE TABLE auth_user ( id int PRIMARY KEY );
CREATE TABLE links_chatpicmessage ();
I\'m trying to add a
The CONSTRAINT
clause is optional. I suggest ommiting it and always letting PostgreSQL autoname the constraint, without naming it you'll get a logical name
"links_chatpicmessage_sender_fkey" FOREIGN KEY (sender) REFERENCES auth_user(id)
That's what you'll likely want to know if an INSERT
or UPDATE
fails due to a constraint violation.
All of these are somewhat documented on ALTER TABLE
ALTER TABLE links_chatpicmessage
ADD COLUMN sender int,
ADD [CONSTRAINT foo] FOREIGN KEY (sender) REFERENCES auth_user(id);
This is compound and transactional. You can issue two ALTER
statements on the same table by separating the two statements with a ,
.
-- assumes someone has already added the column or that it already exists
ALTER TABLE links_chatpicmessage
ADD COLUMN sender int;
ALTER TABLE links_chatpicmessage
ADD [CONSTRAINT foo] FOREIGN KEY (sender) REFERENCES auth_user(id);