Closed patrikrazem closed 4 years ago
Hi, thank you for posting this
I don't have the code up right now, but agreed something seems wrong because indeed the object has not been deleted yet.
I wonder if we didn't have a deletion test case, or if as I assume the exception handler caught, logged, and moved on. We could alter the deletion test to watch for logs created (python or django testing does have tooling for that).
There can be a number of issues with deletion in general (I'm not implying that this issue is one of these), one of which being if you're deletion has a cascade and if the entity that's being deleted relies in some form (it's str or repr) on being able to display a value including data from a related (FK or M2M), that FK or M2M may have been deleted already and thus the CRUDEvent delete record will not succeed (and even the error logger may also fail, probably for the same reason). I may be crossing some wires here (code is in my head), but I just wanted to warn that deletion isn't perfect (both in the library and also via decisions made by the host app). Thank you for creating a sample repo, by the way. I'm going to see if I can look at this within the next week). Please ping me back here if I'm taking too long. My company does use this project so sometimes I'm able to make time during the work day.
Also just making sure, I think we have a new beta (I don't think I've released the official version but I expect it basically is official), would you be willing to upgrade to it if you're not already on it?
Also is this on pre-save or delete? Just want to make sure that the title is correct.
There can be a number of issues with deletion in general (I'm not implying that this issue is one of these), one of which being if you're deletion has a cascade and if the entity that's being deleted relies in some form (it's str or repr) on being able to display a value including data from a related (FK or M2M), that FK or M2M may have been deleted already and thus the CRUDEvent delete record will not succeed (and even the error logger may also fail, probably for the same reason). I may be crossing some wires here (code is in my head), but I just wanted to warn that deletion isn't perfect (both in the library and also via decisions made by the host app). Thank you for creating a sample repo, by the way. I'm going to see if I can look at this within the next week). Please ping me back here if I'm taking too long. My company does use this project so sometimes I'm able to make time during the work day.
I realize that there might be many reasons and complicated scenarios why a delete might fail and that's exactly why I created a sample repo with the minimal possible complexity. It has just one model with one property -- no foreign keys, no relationships, no special configuration.
Also just making sure, I think we have a new beta (I don't think I've released the official version but I expect it basically is official), would you be willing to upgrade to it if you're not already on it?
I'm on django-easy-audit
version 1.2.1
and I have not tested any beta releases yet.
Also is this on pre-save or delete? Just want to make sure that the title is correct.
I don't really understand why, but the exception occurs in the pre_save
handler, as evident in my originally pasted stacktrace.
As far as I can tell:
pre_save
handler is executed, which recieves an instance
of the deleted object. At this point, the instance
(the deleted object) has a pk = NULL
(since it's already been deleted and no longer exists in the DB). CRUDEvent.object_id
has a non-null constraintIt's not quite clear to me why the pre_save
handler is executed before saving the CRUDEvent, but I'm guessing I simply don't sufficiently understand the process.
Anything new on this front?
I'm sorry I haven't had time to debug further.
Please ping me in a week on here if still nothing.
@jheld I'm guessing there's still nothing.
The easyaudit_crudevent
table has an object_id
field which is constrained to being NOT NULL
. When you are storing a CRUD entry for creation and updates, that's simple -- because the object_id
value references the object that was created/edited and is still present in the DB.
But when you delete the object
that the CRUD event is referencing, it can't possibly have an object_id
, because that object no longer exists in the DB. So, of course it doesn't work.
This seems to me to be more of a design decision regarding how the library will handle these scenarios. You could:
deleted
flag, etc.) and always still persist in the DBobject_id
field in the easyaudit_crudevent
table NULLABLE
and thus allow having CRUD event records that don't directly reference the deleted objectTo me it seems like the second option is the most logical one. But then again, I could be wrong.
This has been fixed and is available for on a pre-release version 1.3.0a3. There is a migration because of internal changes to object_id
in the upcoming release but we also don't anticipate any issues.
Going to close this (thank you for opening the issue) since we have a fix in place for the upcoming release which is on PyPI.
Django version: 2.2.11 and 3.0.5 Python version: 3.3.7 Django-easy-audit version: 1.2.1
The following error occurs when attempting to delete an object in a model for which CRUD audit log events are being recorded:
I've created a minimal reproduction repo for this: https://github.com/patrikrazem/easyaudit-sample
As far as I can tell, when the object is deleted, an attempt is made to write a CRUDevent with an
object_id
withNULL
value, because the instance has already been deleted.Is this a bug in the way deletions are handled, or am I missing something (or doing something wrong)?