Home > Enterprise >  Is it bad for columns in composite keys to have mismatching types?
Is it bad for columns in composite keys to have mismatching types?

Time:01-31

Problem: I'd like to make a composite primary key from columns id and user_id for a postgres database table. Column user_id is a foreign key with an integer type, whereas id is a string. Will this cause a conflict because the types are different?

Edit: Also, are there combinations of types that would cause problems?

Context: I obviously should match the type of the User.id field for its foreign key. And, the id for my table will be derived from a uuid to prevent data leaks. So I would prefer not to change the types of either field I want in this table.

Research: I am using sqlalchemy. Their documentation mentions how to create a composite primary key, but it doesn't discuss dealing with different types for each column.

CodePudding user response:

No, this won't be a problem.

Your question seems to indicate that you think, the values of the indexed columns are somehow concatenated and then stored in the index as a single value. This is not the case. Each column value is stored independently but together. Similar to the way the column values are stored in the actual table.

  • Related