Adding a column as a foreign key gives ERROR column referenced in foreign key constraint does not exist

前端 未结 5 563
抹茶落季
抹茶落季 2021-01-31 13:02

I have the following set up,

CREATE TABLE auth_user ( id int PRIMARY KEY );
CREATE TABLE links_chatpicmessage ();

I\'m trying to add a

5条回答
  •  梦如初夏
    2021-01-31 13:52

    I know this answer is way late, and I realize this is the same as btubbs one-liner, just a little more descriptive ...

    Assuming you want to reference the primary key in table auth_user and that key name is 'id'.

    I use this syntax:

    ALTER TABLE links_chatpicmessage 
    ADD COLUMN sender some_type,
    ADD FOREIGN KEY (sender) REFERENCES auth_user(id);
    

    Note: some_type = [type the same as sender in table auth_user]

提交回复
热议问题