django model(s) silently fails to sync to the DB ( for no apparent reason )

109 views
Skip to first unread message

Doug S

unread,
Oct 7, 2013, 9:40:33 PM10/7/13
to django...@googlegroups.com
I don't think I'm making a rookie mistake, I've looked over my code several times.
I've got two pretty simple django models that are just failing to sync to the db
during the syncdb there are no errors, even with --verbosity 3
When I try to make a relation to them I get an error saying the models either don't exist or a re abstract
In fact when I examine the DB, they are not there.
syncdb is attempting to sync models before and after these 2 models in the same file.
I've dropped my DB and started from scratch several times
I don't know how to debug this issue because I don't know how syncdb works under the covers and what can go wrong.
Here are my models, they look pretty simple to me:

class DirEnumVal(models.Model):

    str_val = models.CharField(max_length=64, default='')

    def unicode(self):

        return self.str_val


class DirAttr(models.Model):

    

    BOOL = 'BOOL'

    MONO = 'MONO'

    SCAL = 'SCAL'

    ENUM = 'ENUM'

    

    TYPES = (

        (BOOL,'Boolean'),

        (MONO,'Monomial'),

        (SCAL,'Scalar'),

        (ENUM,'Enumeration'),

    )

    

    val_type = models.CharField(max_length=4, choices=TYPES)

    val_key = models.CharField(max_length=32)

    default_bool = models.BooleanField(default=True)

    default_num = models.IntegerField(default = 0)

    default_float = models.FloatField(default=1.0)

    enum_choices = models.ManyToManyField(DirEnumVal, null=True, blank=True)

    default_choice = models.CharField(max_length=64, default='')

    required = models.BooleanField(default=True)

            

    def unicode(self):

        return '{k} :=> {t} ( default = {d} )'.format(

                k=self.val_key, t=self.val_type, d=self.default())

I am using a pattern with multiple model files for a single app

my dir structure is:

>app

     > models

            - __init__.py 

            - model_file1.py

            - model_file2.py

and in my __init__.py I've got code to pull all the models together into one module:  app.models

      from __future__ import absolute_import

      from .model_file1 import model1a,model1b,model1c

      from .model_file2 import model2a,model2b,model2c

This way of importing the models has been working long before this trouble of models not being synched came up.

When I access the django project and settings through a python shell I can import the models and instatiate them

but when I save them, PostGres gives me this error:

DatabaseError: current transaction is aborted, commands ignored until end of transaction block

This problem surfaced after I installed django-categories and set up some category models.
I had some trouble at first setting that up but I've got the BaseCategory subclasses behaving nicely now
and have started with a fresh DB.
At some point when I was getting django categories to work there was an error when I synched the DB
that said something about an unexcepted special character being somewhere in my code or the django-categories code.
That seemed suspicious but that doesn't show up anymore and I'm using a fresh DB.

Does anybody see anything obvious or know what type of problems can cause syncdb to ignore models?

I'm running out of ideas about what is wrong

I'm on Django 1.5 using PostGreSQL & MacOS Lion

Best Doug

Rene Zelaya

unread,
Oct 8, 2013, 9:08:03 AM10/8/13
to django...@googlegroups.com
Hey Doug,

Any chance you haven't included those models in the INSTALLED_APPS in your settings.py file?

Best,
Rene

David Cox

unread,
Oct 9, 2013, 9:42:43 AM10/9/13
to django...@googlegroups.com
There's a chance that running 'manage.py sqlall' might show errors that syncdb can't display before failing.  'manage.py sqlall' validates the SQL without trying to commit it, so to save time in the future, you should really run it before syncdb every time.

If not, and the app is installed, check import statements, then model field constraints, like trying to assign numbers to 'null' fields or violating 'unique' constraints.

Since the db sync is failing on transaction commit, it sounds more like the latter than the former, though.  Good luck.

David Cox

unread,
Oct 9, 2013, 9:47:21 AM10/9/13
to django...@googlegroups.com
I just rtfc, and it might have been a copy-paste mistake, but the 'str_value' field in your 'DirEnumVal' class has unmatched quotation marks for the default attribute.


On Monday, October 7, 2013 8:40:33 PM UTC-5, Doug S wrote:

Doug S

unread,
Oct 22, 2013, 12:56:12 AM10/22/13
to django...@googlegroups.com

Looks like I found the problem. 
I am using the pattern:

I am using a pattern with multiple model files for a single app

my dir structure is:

>my_app

     > models

            - __init__.py 

            - model_file1.py

            - model_file2.py

and in my __init__.py I've got code to pull all the models together into one module:  app.models

      from __future__ import absolute_import

      from .model_file1 import model1a,model1b,model1c

      from .model_file2 import model2a,model2b,model2c

This pattern is a nice way of putting models into different files when you have a lot of models,
but it also requires you to specify that the models in the different files all belong to the one app
by setting a Meta variable:

class Meta:

        app_label = 'my_app'

I had done this on my previous models when I split the models into separate files

but then added some new models and forgot about that requirement.

Django said the models weren't installed since it didn't know to add them to the right app.

QED


On Monday, October 7, 2013 9:40:33 PM UTC-4, Doug S wrote:
Reply all
Reply to author
Forward
0 new messages