Handling Composite Primary Keys and Junction Tables #804
Replies: 4 comments 6 replies
-
Interesting. In my experience I'm used to seeing (and designing) mapping tables with their own I'd suggest we move forward with the assumption that all primary keys will be single column only. That would imply:
We could add support for composite primary keys later if necessary. |
Beta Was this translation helpful? Give feedback.
-
@kgodey I know we have the issues for supporting multi-column primary-key but I'm not sure how well/(to what depth) we intend to provide support for this right away.
I believe we will create an auto-generated single column primary key for all tables we create, which includes mapping tables.
When connecting to an existing db which contains a table with multi-column primary-key and the user intends to link it to another table, we would essentially have to add all the columns that are part of the primary-key to the mapping table. @kgodey Should we handle this for the initial release? |
Beta Was this translation helpful? Give feedback.
-
@ghislaineguerin We won't be implementing it this way. I explained how we would implement it in #443 but I'll reproduce the relevant tables here: If we start with "Movie" and "Actor" Movie
Actor
The mapping table we create will have the following structure: Movie Actor Link
Here:
Yes.
We don't (at least, not for the initial release). We should show an error message as @seancolsen suggested here. |
Beta Was this translation helpful? Give feedback.
-
While it is reasonable in many cases to use the two foreign keys in a junction table as a composite primary key, I think it would make the schema design less extendable for the user. In case a junction table grows into an entity in its own right, we'll want that primary key We should almost certainly automatically add One advantage I can think of for making the pair a composite primary key would be that it makes it more obvious that the |
Beta Was this translation helpful? Give feedback.
-
Problem
For our most basic use cases, single-column foreign keys are enough to solve basic table linking problems. However, when creating many-to-many relationships, we generate a junction table. Based on research, in some cases, the primary key of a junction table is a composite of both linked tables' foreign keys.
Auto-generated IDs for all tables
For tables created within Mathesar, the system will automatically add a read-only column, assign it as a primary key and generate new unique values every time a record is added. However, in the case of junction tables, should we still generate an additional ID or create a composite PK from both referenced columns?
We'd have something like the table below. Where new records aren't saved unless both columns have values added.
Referencing a composite primary-key
If a user then wants to link this table to another, using the 'Link Table' functionality, would we add a composite foreign key, meaning we insert two columns rather than one?
Nothing is preventing users from selecting a table with a composite primary key for linking, so I think we need to address this.
Questions
Beta Was this translation helpful? Give feedback.
All reactions