I’ve added a ‘version’ column to a table that needs to be part of the table’s primary key, but I’ve got a foreign key relationships to tables that don’t contain the version. (And shouldn’t) I’m sure I’m going to get at least one answer explaining why this isn’t possible, I get why I can’t create a relationship here. I’m looking for an elegent/painless way around it. The behavior I need is that the main table can add versions while the ‘other table’ remains linked to all of them. This is effectively a many-to-many relationship without a join table. (However in practice only one ‘version’ of the main table for a given key is active at any given time.) I plan to enforce my own referencial integrity using a mix of check constraints and triggers…but is there a better way? Thanks
TABLE
TableID (uniqueidentifier) - PRIMARY KEY
Version (int) - PRIMARY KEY
...
OTHERTABLE
OtherTableID (int) - PRIMARY KEY
TableID (uniqueidentifier)
...
I think it would be better to split
TABLEinto two tables: one with the data that never change (which may just be the ID itself) and one with the data that need to be versioned.OTHERTABLEwill have a foreign key to the first one.(I should mention that we’ve used this approach where I work, and it’s sometimes caused annoying complications, especially when we’ve wanted to change our minds about which fields need to be versioned. It’s really not perfect. But other approaches tend to result in even bigger problems.)