Foreign keys referring other foreign keys in PostgreSQL

前端 未结 2 1585
南笙
南笙 2021-01-24 23:56

In PostgreSQL I have a database, which I intend to make the following table declaration:

CREATE TABLE canvas_user (
    id INTEGER,
    login_id VARCHAR(50) UNIQ         


        
2条回答
  •  终归单人心
    2021-01-25 00:45

    A foreign key constraint does not care whether the referenced column(s) is referencing another column itself. But the referenced column(s) must be unique. That's what the error message tells you (quite clearly).

    What you are missing is that a foreign key constraint can be based on multiple columns. This should work:

    FOREIGN KEY (num, user_id, assignment_id) REFERENCES submission
    

    Replacing:

    FOREIGN KEY (num) REFERENCES submission(num),
    FOREIGN KEY (user_id) REFERENCES submission(user_id),
    FOREIGN KEY (assignment_id) REFERENCES submission(assignment_id)

    The short form of the syntax (REFERENCES submission) is possible, because you are referencing the primary key, which is the default.

    Plus, you can simplify: make submission.num the sinlge-column primary key, drop the redundant columns user_id and assignment_id from correction and reduce the fk constraint to just (num) - as discussed in @Tim's answer.

    As long as you have the multicolumn fk constraint, consider NOT NULL constraints on each of the referencing columns (as commented by @joop). Else, one or more NULL values in the referencing columns allow to escape the fk constraint with the default MATCH SIMPLE behaviour. This may or may not be intended, typically it is not.
    Alternatively consider MATCH FULL for multicolumn fk constraints to only allow that if all referencing columns are NULL. Details:

    • MATCH FULL vs MATCH SIMPLE

提交回复
热议问题