1

I have a django app that has been working okay and all of a sudden something seems to be broken.

class ClosedUserGroup(models.Model):
    """ Preset definitions for ClosedUserGroup:
    """
    is_active = models.BooleanField(default=True)
    is_deleted = models.BooleanField(default=False)
    created_at = models.DateTimeField(
        auto_now=True,
        auto_now_add=True,
        null=True,
        default='2013-08-15 13:37:13.370030'
    )
    name = models.CharField(max_length=256, default='<MISSING:CUG_NAME')
    description = models.CharField(max_length=256)


class Partner(models.Model):
    id = models.IntegerField(primary_key=True)
    is_active = models.BooleanField(default=True)
    is_deleted = models.BooleanField(default=False)
    created_at = models.DateTimeField(
        auto_now=True,
        auto_now_add=True,
        null=True,
        default=datetime.datetime.now()
    )
    name = models.CharField(max_length=256, default='<MISSING:PARTNERNAME>')

class PartnerCug(models.Model):
    """ Partner to ClosedUserGroup relation
    """
    is_active = models.BooleanField(default=True)
    is_deleted = models.BooleanField(default=False)
    created_at = models.DateTimeField(
        auto_now=True,
        auto_now_add=True,
        null=True,
        default='2013-08-15 13:37:13.370030'
    )
    partner = models.ForeignKey(Partner)
    cug = models.ForeignKey(ClosedUserGroup)        

class Account(models.Model):
    """
        Account:
        TODO:DESCRIPTION
    """
    id = models.IntegerField(primary_key=True)
    is_active = models.BooleanField(default=True)
    is_deleted = models.BooleanField(default=False)
    created_at = models.DateTimeField(
        auto_now=True,
        auto_now_add=True,
        null=True,
        default=datetime.datetime.now()
    )
    number = models.CharField(max_length=64)
    submitted_by = models.ForeignKey(
        Partner,
        db_column='submitted_by'
    )       

in django shell, the following commands run successfully and returns a result of 1:

from myapp.models import Account
account_object = Account.objects.filter(id=1)
account_object.filter(account__submitted_by__partnercug__cug=3).count()

However, in django itself, I have the function

def get_other_accounts():
    """get total accounts"""
    account_object = Account.objects.filter(id=1)
    total_accounts = account_object.filter(account__submitted_by__partnercug__cug=3).count() 

This however fails with the error DatabaseError: current transaction is aborted, commands ignored until end of transaction block and it points to the last line in that function. So its working django shell but not from the web app.

lukik
  • 3,919
  • 6
  • 46
  • 89
  • Tried after exiting shell? – Rohan Oct 26 '13 at 10:10
  • look what you got in the two filters, it's not different ? – drabo2005 Oct 26 '13 at 10:12
  • You know that in the shell you are counting `PartnerCug` objects and in the Django function you are counting `ClosedUserGroup` (cug) objects, whatever those might be? – frnhr Oct 26 '13 at 10:14
  • 3
    first query have `account__submitted_by__partnercug` and second one have `account__submitted_by__partnercug` **__cug** – Mp0int Oct 26 '13 at 11:40
  • @FallenAngel I've edited that. It was a typo. Error still stands – lukik Oct 27 '13 at 02:51
  • @frnhr the query should return the number of accounts submitted by other entities in closed user group with that primary ID – lukik Oct 27 '13 at 02:52
  • My point was exactly the same as FallenAngel's, no nothing of it... – frnhr Oct 27 '13 at 14:18
  • What database are you using? – George Stocker Oct 27 '13 at 14:55
  • Possible dup of http://stackoverflow.com/questions/2979369 – Fiver Oct 27 '13 at 14:56
  • @GeorgeStocker using Postgresql – lukik Oct 27 '13 at 18:40
  • @Pathétique hmmm. thanks for that. Now checking to see if there are any error further up. But duplicate? In this case the code works in Django Shell but not in the webapp. – lukik Oct 28 '13 at 06:52
  • @lukik the error message is the same, the problem is the same. Somewhere in your web application you have code that is throwing an error in the database. The database is telling you, "Hey, I'm not doing anything else because I'm in an error state." – George Stocker Oct 28 '13 at 11:10

1 Answers1

0

Missing id or pk field (primary key) on ClosedUserGroup model?

I don't see how that could work in either shell or web.

frnhr
  • 12,354
  • 9
  • 63
  • 90
  • The model didn't have but the fixtures had it so in the DB the primary ID is there. – lukik Oct 28 '13 at 06:49
  • Add it to the model then? Django shouldn't know of any DB columns that are not described by a `Field`, they could only cause problems – frnhr Oct 28 '13 at 16:38