Question

I have a models folder that has a few models in files that are already in the DB. I have just added another file/model but it is not being added to the DB when I run syncdb. I've tried manage.py validate and it is running fine. I have also run the code and it only fails when it tries to save with "table does not exist".

the original structure was like this:
/models
-- __init__.py
-- file1.py
-- file2.py

and __init__.py looked like:

from file1 import File1Model
from file2 import File2Model

I added file3.py
/models
-- __init__.py
-- file1.py
-- file2.py
-- file3.py

and modified __init__.py

from file1 import File1Model
from file2 import File2Model
from file3 import File3Model

And the contents of file3 (names may have been changed to protect the innocent, but besides that its the exact file):
UPDATE: just tried adding a primary key since the id field may have been messing with the automatically added integer primary key id. Also tried a few variations but no dice.

from django.db import models
from django.contrib.auth.models import User


class File3Model(models.Model):
    user = models.OneToOneField(User)
    token = models.CharField(max_length=255, blank=False, null=False)
    id = models.CharField(primary_key=True, max_length=255)

    class Admin:
        pass

    class Meta:
        app_label = 'coolabel'

    def __unicode__(self):
        return self.user.username

    @staticmethod
    def getinstance(user, token, id):
        try:
            instance = File3Model.objects.get(pk=id)
            if instance.token != token:
                instance.token = token
                instance.save()
            return instance
        except:
            pass
        instance = File3Model()
        instance.user = user
        instance.token = token
        instance.id = id
        instance.save()
        return instance

So in this example, File1Model and File2Model are already in the DB and remain in the DB after syncdb. However, File3Model is not added even after rerunning syncdb. Is there any way to figure out why the new model isn't being added??

Was it helpful?

Solution 4

BOOM!

I was using a different app_label for the new model but it has to be the same across the model group.

The other models labels were "mediocrelabel" and my new model had the label "coolabel". I changed the new model's label to "mediocrelabel" and now they are being added to the DB correctly.

Thanks for your help, folks!

OTHER TIPS

If you define the model outside of models.py, you have to set the app_label attribute on the models Meta class.

Edit: The app_label has to refer to an app in your INSTALLED_APPS setting. It should probably match the name of the app that the models directory is in, unless you've got a really good reason to do otherwise. That seems to have been your problem here.

class File3Model(models.Model):
    foo = models.CharField(...)
    ...

    class Meta:
        app_label = "my_app"

Note that syncdb will never remove any tables from the db. The other tables were probably created with syncdb before the models.py was replaced with the directory structure.

set app_label to my app solves my problem.

Why did you split you models and having models folder instead of placing models in models.py ?

In my own project there are about 10 models live in models.py and I'm fine with it.

You can also try manage.py syncdb --all.

And I think its better to keep all models in one single file and import them like from my_app.models import model_name instead of keeping in mind to import necessary model into models/__init__.py. By the way you avoid many problems and long lined imports and don't care about where some_model lives among models/*.py files.

Thanks,

Sultan

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top