Search code examples
pythondjangodjango-modelsdjango-syncdb

manage.py syncdb doesn't add tables for some models


My second not-so-adept question of the day: I have a django project with four installed apps. When I run manage.py syndb, it only creates tables for two of them. To my knowledge, there are no problems in any of my models files, and all the apps are specified in INSTALLED_APPS in my settings file. Manage.py syndb just seems to ignore two of my apps.

One thing that is unique about the two "ignored" apps is that the models files import models from the other two apps and use them as foreign keys (don't know if this is good/bad practice, but helps me stay organized). I don't think that's the problem though, because I commented out the foreign key-having models and the tables still weren't created. I'm stumped.

UPDATE: When I comment out the lines importing models files from other apps, syndb creates my tables. Perhaps I'm not understanding something about how models files in separate apps relate to other other. I though it was ok to use a model from another app as a foreign key by simply importing it. Not true?


Solution

  • Unfortunately, manage.py silently fails to load an app where there's an import error in its models.py (ticket #10706). Chances are that there's a typo in one of your models.py files... check all of the import statements closely (or use pylint).

    Recently syncdb stoped loading a couple of my apps, and sqlall gave me the error "App with label foo could not be found". Not knowing that this sometimes means "App with label foo was found but could not be loaded due to ImportError being raised", it took me half an hour to realise that I was trying to import 'haslib' instead of 'hashlib' in one of my models.py files.