1 Map has N Images. When a the user updates the Map data I do not make an update because then I lose the old data. Thus I do an Insert into the Map table with the same auto incremented Id but a new datetime stamp. This way I want to historize all changes on a Map in the user interface.
Table Map: PK is Id + CreatedAt
[Id] [int] IDENTITY(1,1) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
...
Table Images: PK is Id, FK is MapId + CreatedAt
[Id] [int] IDENTITY(1,1) NOT NULL,
[Image] [varbinary](max) NOT NULL,
[VisibleIndex] [int] NOT NULL,
[CreatedAt] [datetime] NOT NULL,
[MapId] [int] NOT NULL,
If I have a PK with 2 fields my FK must also have 2 fields.
But this does not work in my case because the CreatedAt date from table Images cant be the createdAt date from the table Map.
How would you do the historical “update” ?
Separate “constant” and “evolvable” aspects of the map into two tables:
This assumes you only want to version the map fields but not images.
If you also want to version images, you could do it like this:
When creating a new Map version:
This way, you don’t have to do expensive copying of the image content just because the map has changed. You only create a new image version when the image itself has changed.
NOTE: This model is a little too general. It allows same images to be shared between multiple maps (not just multiple versions of the same map). Let me know if you’d like to restrict that.
To make images “private” to the map, you can do something like this:
Note how we don’t introduce any surrogate keys. Instead, we let identifying relationships produce “natural” keys, which then get merged at the bottom of the diamond-shaped dependencies:
ImageLink.MapIdhas foreign keys toward both “edges” of the “diamond”