I have a database with multiples tables that must be audited.
As an example, I have a table of objects defined with a unique ID, a name, and a description.
The name will always be the same. It is not possible to update it. "ObjectA" will always be "ObjectA".
As you see the name is not unique in the database but only in the logic.
The rows "from", "to" and "creator_id" are used to audit the changes. "from" is the date of the change, "to" is the date when a new row has been added and is null when it is the latest row. "creator_id" is the ID of the user that made the change.
+----+----------+--------------+----------------------+----------------------+------------+| id | name | description | from | to | creator_id |+----+----------+--------------+----------------------+----------------------+------------+| 1 | ObjectA | My object | 2021-05-30T00:05:00Z | 2021-05-31T05:04:36Z | 18 || 2 | ObjectB | My desc | 2021-05-30T02:07:25Z | null | 15 || 3 | ObjectA | Super object | 2021-05-31T05:04:36Z | null | 20 |+----+----------+--------------+----------------------+----------------------+------------+
Now I have another table that must have a foreign key to this object table based on the "unique" object name.
+----+---------+-------------+| id | foo | object_name |+----+---------+-------------+| 1 | blabla | ObjectA || 2 | wawawa | ObjectB |+----+---------+-------------+
How can I create this link between those 2 tables?
I already tried to create another table with a UUID and add a column "unique_identifier" in the object table. The foreign key will be then linked to this UUID table and not the object table. The issue is that I have multiple tables with this problem and I will have to create a double number of tables.
It is also possible to use the object ID as the FK instead of the name but it would mean that I must update every table with that FK with the new ID when updating an object.