question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bulk deletion of subclass objects not working

See original GitHub issue

If I create a series of models roughly like this:

class A(PolymorphicModel):
some fields ...

class B(A):
# no extra fields... just new methods

class C(A):
# no extra fields... just new methods
....

then create some objects:

B().save()
C().save()

and then try and delete them:

A.objects.all().delete()

then I get an error something like “‘C’ object has no attribute ‘b_ptr’”

Whereas if I delete them individually:

[i.delete() for i in A.objects.all()] 
>[None, None]

then all is fine. I’m not sure if this is a bug or a feature, but it’s frustrating because I’m going to have to override the admin to make deletion of inline objects work (I’m using B and C only to override methods on A, no additional fields are stored).

Issue Analytics

  • State:open
  • Created 12 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
carljmcommented, Apr 13, 2012

I’m not sure what code this applies to, since there is no PolymorphicModel in django-model-utils and never has been.

0reactions
spatialbitscommented, Jul 15, 2016

yeah sorry, could have added that. I am finding that it’s not just on a bulk delete. When I set published to False on a model instance and save it I get an error as per below:

>>> from heritage.models import HeritageAsset, SessionAsset
>>> s = SessionAsset.objects.get(id=5993)
>>> s.published=False
>>> s.save()

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/base.py", line 761, in save_base
    self._save_parents(cls, using, update_fields)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/base.py", line 786, in _save_parents
    self._save_table(cls=parent, using=using, update_fields=update_fields)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/base.py", line 846, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/base.py", line 885, in _do_insert
    using=using, raw=raw)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 974, in execute_sql
    cursor.execute(sql, params)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/data/projects/overview-env/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "heritage_heritageasset_pkey"
DETAIL:  Key (secureasset_ptr_id)=(5993) already exists.

Model Definitions:

class AssetAbstract(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False)
    published = models.BooleanField(default=True)

    objects = PublishedAssetInheritanceObjectManager()
    admin_objects = InheritanceManager()

    class Meta:
        abstract = True

class SecureAsset(AssetAbstract):
    file = models.FileField()
    class Meta:
        permissions = (
            ("view_secureasset", "Can view secure asset"),
        )

class HeritageAsset(SecureAsset):
    @property
    def storage_string(self):
        return "heritage_assets"

class SessionAsset(HeritageAsset):
    session = models.ForeignKey(Session)

And the object manager as posted above:

class PublishedAssetInheritanceObjectManager(InheritanceManagerMixin, models.Manager):
    def get_queryset(self):
        return super(PublishedAssetInheritanceObjectManager, self).get_queryset().filter(published=True)

Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError Exception raised when trying to bulk delete ...
I'm trying to bulk delete all of the comments on a dev instance of my Django website and Django is raising an AttributeException....
Read more >
Chapter 7. Batch processing - Red Hat on GitHub
However, Hibernate provides methods for bulk SQL-style UPDATE and DELETE statement execution which are performed through JP-QL (Chapter 8, JP-QL: The Object ......
Read more >
Common Hibernate Exceptions - Baeldung
Many conditions can cause exceptions to be thrown while using Hibernate. These can be mapping errors, infrastructure problems, SQL errors, ...
Read more >
Models - Django documentation
Be careful not to choose field names that conflict with the models API like clean , save , or delete . Example: from...
Read more >
Working with Objects - Doctrine
Removing entities · Just like persist , invoking remove on an entity does NOT cause an immediate SQL DELETE to be issued on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found