Skip to content
Advertisement

multiple foreign keys as primary key postgres, should I do it?

This is one of those: why I should or why I should not.

So my books app has reviews, but one user must not review the same book more than one time. In my point of view makes sense to create a table for the reviews and make the user_id and the book_id (ISBN) as the PRIMARY KEY for the reviews table. But, could it be a problem at some point if the application gets too many reviews, for example, could that decision slow the queries?

I am using postgres and I am not sure if the following code is correct:

Advertisement

Answer

This is a perfectly valid design choice.

You have a many-to-many relationship between books and users, which is represented by the reviews table. Having a compound primary key based on two foreign keys lets you enforce referential integrity (a given tuple may only appear once), and at the same time provide a primary key for the table.

Another option would be to have a surrogate primary key for the bridge table. This could make things easier if you need to reference the reviews from another table, but you would still need a unique constraint on both foreign key columns for integrity, so this would actually result in extra space being used.

When it comes to your code, it has a few issues:

  • the primary key keyword goes after the datatype

  • the check constraint is incorrectly formed

  • missing or additional commas here and there

Consider:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement