Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'revision.revert(delete=True)' should delete existing objects not present in the version to which we are reverting, but its not deleting the extra objects #975

Open
Prakhar1247 opened this issue Aug 20, 2024 · 0 comments

Comments

@Prakhar1247
Copy link

I have three models defined as:-

@reversion.register()
class Transaction(models.Model):
    trackid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    data_group = models.CharField(max_length=255)

@reversion.register()
class Transaction_Quest(models.Model):
    trackid = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction_records')
    questionid = models.ForeignKey(Questions_List, on_delete=models.CASCADE, related_name='question_details')
    question_status = models.CharField(max_length=10)
    question_value = models.DateField(default=None, null=True)
    
@reversion.register()
class Transaction_Ques_Attachment(models.Model):
    trackid = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name='transaction_id')
    ques_id = models.ForeignKey(Transaction_Question, on_delete=models.CASCADE, related_name='trans_ques_records')
    filename = models.CharField(max_length=50)
    file_description = models.TextField()

A Transaction can be associated to multiple Questions (Transaction_Quest) and a Question can be associated to multiple attachments (Transaction_Ques_Attachment)

So when I am creating a new entry for these models under with reversion.create_revision(): block, so a new version is created for each of the three models with same revision id.
Example:-
A user created a record containing:-

  • 1 object for Transaction model
  • 2 objects for Transaction_Quest model with id=1 and id=2
  • 2 objects for Transaction_Ques_Attachment with id=1 and id=2 associated to 1st object (id=1) of Transaction_Quest model, while 2nd object of Transaction_Quest model is not associated to any object of Transaction_Ques_Attachment.

So in total there are 5 version entries created with same revision_id = 1 as-
1 version for Transaction model, 2 versions for Transaction_Quest model and 2 versions for Transaction_Ques_Attachment model.

Updating same record under with reversion.create_revision(): block, so again a new version is created for each of the three models with same revision id as below:-
Now this time user updated the above record and this time he added 2 more new objects in the Transaction_Ques_Attachment model associated to same 1st object (id=1) of Transaction_Quest model and updated some data in the object of Transaction model and 2nd object of Transaction_Quest model.
So now this time 7 version entries created with same revision_id = 2 which now is also the selected version.
1 version for Transaction model, 2 versions for Transaction_Quest model and 4 versions for Transaction_Ques_Attachment model associated to 1st object (id=1) of Transaction_Quest model.

Logic to revert back to any specific version

Now for reverting to previous version i.e. revision_id=1 from revision_id=2, I am using below logic and I have passed delete=True:-

from reversion.models import Revision
revision = Revision.objects.get(id=1)
revision.revert(delete=True)

So after reverting to previous version i.e. revision_id=1 extra 2 objects present in revision_id=2 for model Transaction_Ques_Attachment should be deleted as version 1 does not have these objects. But even after using delete=True option those extra 2 objects are not getting deleted from the Transaction_Ques_Attachment model. Though other records are reverted back to version 1 but the extra 2 records which were added in version 2 are still there in the Transaction_Ques_Attachment model even after reverting back to Version 1.

So need help on this issue as to why revision.revert(delete=True) is not working as expected and why the extra records not getting deleted from the Transaction_Ques_Attachment model even after reverting back to 1st revision?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant