0

I have a very strange problem.

I'm trying to delete some feeds (model name: Feed) from my django model. So I run:

    from rss.models import Feed
    from django.db.models import Count
    feeds = Feed.objects.annotate(num_subs=Count('subscriptions')).filter(num_subs=0)

    for feed in feeds:
       feed.delete()

And sure enough, this deletes the records from my database. In postgres, I did a query:

    rs3=# select count(*) from rss_feed;
     count 
    -------
      1528
    (1 row)

But then the strangest thing happens. I ran the count query again and again, and the number of feeds kept growing (without any users manually adding them), until it went back to its original number. And I've repeated this exercise a few times. How is this possible, and how do I permanently delete these records?

1
  • Do you have any pre_delete/post_delete signals or a custom delete method on Feed? These might be preventing the delete. Also, what version of Django, and are you using transactions that are rolling back on an error? Commented Dec 16, 2013 at 9:29

2 Answers 2

1

Databases seldom auto-populate themselves out of thin air and fresh water, so you very probably have some code somewhere that creates Feed instances (signal, cron job, whatever, doesn't even have to be python code).

Sign up to request clarification or add additional context in comments.

1 Comment

This is what was happening: A celery task triggered by cron was taking too long, so it had a backlog in rabbitmq. Those tasks contained ORM representations of the feeds, so even if I deleted the feeds from the database, when a task (initiated who knows how long ago before sitting in the queue) would finish executing, it was saving the feeds again. After clearing rabbitmq completely, I could delete the feeds.
0

try:

feeds = Feed.objects.annotate(num_subs=Count('subscriptions')).filter(num_subs=0).all().delete()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.