{"action":"created","issue":{"url":"https://api.github.com/repos/scoursen/django-softdelete/issues/30","repository_url":"https://api.github.com/repos/scoursen/django-softdelete","labels_url":"https://api.github.com/repos/scoursen/django-softdelete/issues/30/labels{/name}","comments_url":"https://api.github.com/repos/scoursen/django-softdelete/issues/30/comments","events_url":"https://api.github.com/repos/scoursen/django-softdelete/issues/30/events","html_url":"https://github.com/scoursen/django-softdelete/issues/30","id":174675231,"number":30,"title":"Failing to see if row already exists?","user":{"login":"Tatsh","id":724848,"avatar_url":"https://avatars.githubusercontent.com/u/724848?v=3","gravatar_id":"","url":"https://api.github.com/users/Tatsh","html_url":"https://github.com/Tatsh","followers_url":"https://api.github.com/users/Tatsh/followers","following_url":"https://api.github.com/users/Tatsh/following{/other_user}","gists_url":"https://api.github.com/users/Tatsh/gists{/gist_id}","starred_url":"https://api.github.com/users/Tatsh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tatsh/subscriptions","organizations_url":"https://api.github.com/users/Tatsh/orgs","repos_url":"https://api.github.com/users/Tatsh/repos","events_url":"https://api.github.com/users/Tatsh/events{/privacy}","received_events_url":"https://api.github.com/users/Tatsh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-02T03:53:04Z","updated_at":"2016-09-19T19:09:24Z","closed_at":null,"body":"This is a little hard to fully explain as I do not know the exact cause of the issue. I am getting an INSERT where an UPDATE should be occurring.\r\n\r\nThe model is like this:\r\n\r\n```python\r\nclass Address(SoftDeleteObject):\r\n    field_name = models.CharField(max_length=30)\r\n\r\nclass UserDetails(SoftDeleteObject):\r\n    address = models.OneToOneField(Address, on_delete=models.CASCADE, blank=True, null=True)\r\n```\r\n\r\nThe data processing is through model formsets, like so:\r\n\r\n```python\r\nif form.is_valid():\r\n    user = form.save(commit=False)\r\n\r\n    if details_formset.is_valid() and address_formset.is_valid():\r\n        user.save()\r\n        form.save_m2m()\r\n        details_formset.save()\r\n        address_formset.save()\r\n```\r\n\r\nAbout 6 times in a week, this error has popped up:\r\n\r\n```\r\nIntegrityError: duplicate key value violates unique constraint \"game_address_user_id_key\"\r\nDETAIL:  Key (user_id)=(3924) already exists.\r\n```\r\n\r\nStack trace:\r\n\r\n```\r\n  File \"django/core/handlers/base.py\", line 132, in get_response\r\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\r\n  File \"django/views/generic/base.py\", line 71, in view\r\n    return self.dispatch(request, *args, **kwargs)\r\n  File \"django/views/generic/base.py\", line 89, in dispatch\r\n    return handler(request, *args, **kwargs)\r\n  File \"django/utils/decorators.py\", line 34, in _wrapper\r\n    return bound_func(*args, **kwargs)\r\n  File \"django/contrib/auth/decorators.py\", line 22, in _wrapped_view\r\n    return view_func(request, *args, **kwargs)\r\n  File \"django/utils/decorators.py\", line 30, in bound_func\r\n    return func.__get__(self, type(self))(*args2, **kwargs2)\r\n\r\n  File \"game/views/account.py\", line 76, in post\r\n    address_formset.save()\r\n\r\n  File \"django/forms/models.py\", line 640, in save\r\n    return self.save_existing_objects(commit) + self.save_new_objects(commit)\r\n  File \"django/forms/models.py\", line 771, in save_new_objects\r\n    self.new_objects.append(self.save_new(form, commit=commit))\r\n  File \"django/forms/models.py\", line 904, in save_new\r\n    obj.save()\r\n  File \"game/models.py\", line 196, in save\r\n    super(Address, self).save(*args, **kwargs)\r\n  File \"softdelete/models.py\", line 244, in save\r\n    super(SoftDeleteObject, self).save(**kwargs)\r\n  File \"django/db/models/base.py\", line 710, in save\r\n    force_update=force_update, update_fields=update_fields)\r\n  File \"django/db/models/base.py\", line 738, in save_base\r\n    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)\r\n  File \"django/db/models/base.py\", line 822, in _save_table\r\n    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)\r\n  File \"django/db/models/base.py\", line 861, in _do_insert\r\n    using=using, raw=raw)\r\n  File \"django/db/models/manager.py\", line 127, in manager_method\r\n    return getattr(self.get_queryset(), name)(*args, **kwargs)\r\n  File \"django/db/models/query.py\", line 920, in _insert\r\n    return query.get_compiler(using=using).execute_sql(return_id)\r\n  File \"django/db/models/sql/compiler.py\", line 974, in execute_sql\r\n    cursor.execute(sql, params)\r\n  File \"raven/contrib/django/client.py\", line 112, in execute\r\n    return real_execute(self, sql, params)\r\n  File \"django/db/backends/utils.py\", line 64, in execute\r\n    return self.cursor.execute(sql, params)\r\n  File \"django/db/utils.py\", line 97, in __exit__\r\n    six.reraise(dj_exc_type, dj_exc_value, traceback)\r\n  File \"django/db/backends/utils.py\", line 64, in execute\r\n    return self.cursor.execute(sql, params)\r\n```"},"comment":{"url":"https://api.github.com/repos/scoursen/django-softdelete/issues/comments/248091796","html_url":"https://github.com/scoursen/django-softdelete/issues/30#issuecomment-248091796","issue_url":"https://api.github.com/repos/scoursen/django-softdelete/issues/30","id":248091796,"user":{"login":"AndySun25","id":7091063,"avatar_url":"https://avatars.githubusercontent.com/u/7091063?v=3","gravatar_id":"","url":"https://api.github.com/users/AndySun25","html_url":"https://github.com/AndySun25","followers_url":"https://api.github.com/users/AndySun25/followers","following_url":"https://api.github.com/users/AndySun25/following{/other_user}","gists_url":"https://api.github.com/users/AndySun25/gists{/gist_id}","starred_url":"https://api.github.com/users/AndySun25/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndySun25/subscriptions","organizations_url":"https://api.github.com/users/AndySun25/orgs","repos_url":"https://api.github.com/users/AndySun25/repos","events_url":"https://api.github.com/users/AndySun25/events{/privacy}","received_events_url":"https://api.github.com/users/AndySun25/received_events","type":"User","site_admin":false},"created_at":"2016-09-19T19:09:24Z","updated_at":"2016-09-19T19:09:24Z","body":"I think I found the issue, note that I haven't had time yet to test with this library (only tested with safedelete, but the theory is identical judging by the code base).\r\n\r\nIn my case the actual issue was masked by my use of django-polymorphic, django will first update parent tables when updating objects spread across relations. After abandoning third party logical delete solutions I implemented my own in-house safe delete mixin/model and found the issue.\r\n\r\nhttps://github.com/django/django/blob/master/django/db/models/base.py#L891\r\nHere you can see that an during update the manager is instantiated to locate the update object, however something has changed that causes soft-deleted objects to be discarded from this queryset; when django is unable to find the soft deleted object it attempts to insert a new record at which point an integrity error will be thrown at the database level.\r\n\r\nTry overriding the `get_queryset()` method here and disregard `deleted_at` and test soft deleting again. While this certainly isn't the solution it should help identify the root of the issue.\r\nhttps://github.com/scoursen/django-softdelete/blob/master/softdelete/models.py#L104"}}
{"actor":{"display_login":"AndySun25"}}