Open ledermann opened 8 years ago
Consider the following scenario to understand why the current implementation of the gem is fatal:
int(11) NOT NULL AUTO_INCREMENT
(pretty standard)auditable_id == 100
. Both audits violate referential integrity, because there is no document with ID 100 anymore.SELECT MAX(id)+1 FROM documents
. It will be 99+1 == 100.auditable_id == 100
Now, Document.find(100).audits.count == 3
, which is a serious bug, because the newly created document is linked with unrelated audits.
Currently I stumbled upon a serious issue using audited with MySQL.
MySQL doesn't manage persistent AUTOINCREMENT values for InnoDB tables: After a server restart, MySQL calculates the next value by doing a
SELECT MAX(id)+1 FROM table
. Because of this, a value can be reused if you delete the last inserted record. Since 10 years ago there is a bugreport for this.This is a problem with audited, because in its current implementation it creates orphans if a record gets destroyed (audit record associated with a non-existing auditable / associated).
A possible solution could be:
dependent: :nullify
for bothhas_many
associations:auditable
to NULL for audits withaction == 'destroy'
What do you think? Are you open for a PR to fix this issue?