Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 819
twneale Avatar asked Dec 09 '22 19:12

twneale


2 Answers

I think I ran across something similar.

I had an issue where a model wasn't being reset. In this case it turned out that there was an error in my models that wasn't being spit out.

Although I think syncdb, when run, spit out some kind of error.

In any case try to import your models file from the shell and see if you can.

$ manage.py shell
>>> from myapp import models
>>>

If theres an error in the file this should point it out.

According to your update, it sounds like you may have a cross-import issue. Instead of:

from app1.models import X

class ModelA(models.Model):
    fk = models.ForeignKey(X)

Try:

class ModelA(models.Model):
    fk = models.ForeignKey("app1.X")

... although I think you should get an error on syncdb.

like image 195
monkut Avatar answered Dec 28 '22 12:12

monkut


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.

like image 32
ozan Avatar answered Dec 28 '22 11:12

ozan