Search code examples
pythondjangodjango-oscar

Django-oscar : deleting a voucher don't delete the associated "ConditionalOffer"


I'm facing a problem with Django-Oscar wile trying to play with offers and vouchers. When I delete a voucher the related entity ConditionalOffer is not deleted and remain in database.

So when I try to add another voucher with the same code as the one I just deleted I get a beautiful IntegrityError duplicate key value violates unique constraint "offer_conditionaloffer_name_key"

I'm using django-oscar (2.0.1) with PostgreSQL (11) and psycopg2 (2.8.3).

Here a screenshot of the error and another of the ConditionalOffer table with entries (10off and 20off) remaining there.

Is that normal behaviour or a bug? If it's normal how can I do to create a voucher with the same code as an old one already deleted ?


Solution

  • This is normal (if somewhat undesirable) behaviour - Oscar doesn't clean up offers that were created with a voucher. The reason for this is that as far as the models are concerned, an offer can be associated with any number of vouchers - so it doesn't assume that when a voucher is deleted the offer is no longer needed.

    There is probably an argument that if an offer is created for a voucher through the dashboard, and shares the same name, then it should be deleted when the voucher is - but this isn't currently implemented.

    For now you will need to delete the orphaned offer either from the Django admin, or from the shell.