Django not creating db tables for models (neither with syncdb nor South)

11,715

Solution 1

Run the following commands

python manage.py makemigrations yourappname

python manage.py migrate

Note it works on django 1.7 version for me.

Solution 2

you're misunderstanding the process of working with south. South isn't just another application, it's a managing tool. Your app needs to be a South application from the begining or converted to one. That being said, the process is like so:

  1. Add South to INSTALLED_APPS
  2. run syncdb for the first time
  3. Add your app to INSTALLED_APPS*
  4. run the south initialization command:

    python manage.py schemamigration myapp --initial
    
  5. migrate:

    python manage.py migrate
    

If you want to convert a project:

  1. Run syncdb after adding south
  2. run:

    manage.py convert_to_south myapp

And use south from now on to manage your migrations.

*p.s. - you can add both south and your own app at the same time, if you keep in mind to put south before your own apps. That's because django reads INSTALLED_APPS in order - it runs syncdb on all apps, but after installing south it won't install the rest and instead tell you to use the south commands to handle those

edit

I misled you. Since you put so much emphasis on the south thing I didn't realize the problem was you were trying to use models as a directory module instead of a normal file. This is a recognized problem in django, and the workaround is actually exactly as you though in the first place:

say this is your structure:

project/
       myapp/
            models/
                  __init__.py
                  bar.py

you need bar.py to look like this:

from django.db import models

class Foo(models.Model):
    # fields...

    class Meta:
        app_label = 'myapp' #you need this!

and __init__.py needs to look like this:

from bar import Foo

Make sure it looks like this and it will work.

UPDATE 18/08/2014

The ticket has changed to wontfix, because apparently the bigger issue with the app_label has been fixed. Huzza!

Share:
11,715
user3227889
Author by

user3227889

Updated on June 12, 2022

Comments

  • user3227889
    user3227889 almost 2 years

    I have a Django project on a Centos VPS.

    I created some models and debugged them so they validate and give no errors. I have them in a "models" folder in my myapp and have added each model to the init file in this directory, for example:

    from category import Category

    I added the app to settings.py INSTALLED_APPS and ran:

    Python manage.py syncdb
    

    It appeared to work fine and added all tables apart from the ones for my app.

    I then installed South and added that to INSTALLED_APPS and, tried syncdb again and ran:

    Python manage.py schemamigration myapp --initial
    

    It generated the file correctly but nothing was in it (none of the tables my models).

    An example file in "models" folder (usertype.py)

    from django.db import models
    
    class UserType(models.Model):
        usertype_id = models.PositiveIntegerField(primary_key=True)
        description = models.CharField(max_length=100)
        is_admin = models.BooleanField()
        is_moderator = models.BooleanField()
    
    class Meta:
        app_label = 'myapp'
    

    Any ideas what could be going wrong here and why I can't get anything to detect my models?

  • user3227889
    user3227889 over 10 years
    I am using --initial because it's a new app with no models in db. I tried "manage.py convert_to_south myapp" just to see what happened and it said "This application has no models; this command is for applications that already have models syncdb'd. Make some models, and then use ./manage.py schemamigration myapp --initial instead." As mentioned, I can run --initial and it executes as expected aside from the fact the generated file has nothing in it, it does not detect any of the models. These are new models none of which are in db.
  • yuvi
    yuvi over 10 years
    1. The way you described it in your post is not the way I described it 2. Can you share models.py? Also any additional information you think is relevant. I don't think I see the entire problem yet
  • user3227889
    user3227889 over 10 years
    I have now removed the references from __init__.py in "models" folder as per your recommendation. Thanks for that, though it didn't change my issue as stated with no models being detected.
  • yuvi
    yuvi over 10 years
    I didn't expect it to change anything, I just mentioned it.
  • user3227889
    user3227889 over 10 years
    Cool, I have now added an example of one of the models to the description of the issue above.
  • yuvi
    yuvi over 10 years
    Ok - looking at your model everything seems fine. Now- here's the thing. If you haven't ran syncdb on south yet, the application isn't a south application. Therefore, it should most definitly create tables for your models.py. So the problem isn't related to south probably, but something else. Are you sure you put the app name in your installed_apps properly? Also- why are you setting your app_label? You don't need to do that unless you want it to be different
  • user3227889
    user3227889 over 10 years
    Yes syncdb (without South) does not work either as mentioned. In INSTALLED_APPS I just have 'myapp' where myapp/models is the relative path (from where manage.py and settings.py is) to my models. I thought app_label was needed, but perhaps not then so I shall remove it.
  • yuvi
    yuvi over 10 years
    it isn't needed, but even so, I created a new test app and everything worked fine with it. I have no idea what you're doing wrong, but it doesn't have anything to do with south
  • user3227889
    user3227889 over 10 years
    This maybe because I haven't installed Django properly. I'm using the system version of Python as I failed to get Django on my altinstall of a later Python version. However when I try to make a new project django-admin.py startproject mytestapp I get -bash: django-admin.py: command not found. I installed Django with yum install Django
  • yuvi
    yuvi over 10 years
    That's probably the problem. Try a fresh install inside an isolated virtualenv, that might do the trick
  • user3227889
    user3227889 over 10 years
    I managed to make a fresh project (it just needed django-admin startproject mytestapp - no .py). I have the same issue with the fresh project
  • yuvi
    yuvi over 10 years
    Django. Try re-installing django
  • user3227889
    user3227889 over 10 years
    I just did a pip install of Django 1.6.1 which replaced 1.4.8 for the system python version I'm using (2.6). This hasn't resolved the issue.
  • yuvi
    yuvi over 10 years
    I really can't what's the problem without more information. I can tell you that it isn't related to south, and probably related to your setup. I suggest you use a virtualenv which creates an isolated version of python and pip. Install everything from scratch there, maybe there's something wrong with your system site-packages...
  • user3227889
    user3227889 over 10 years
    I've followed some instructions online to try and do this (install latest Python fresh with altinstall and make virtualenv). I got a few errors along the way but proceeded. Sadly trying to sudo python virtualenvwrapper-4.2/setup.py just got distutils.errors.DistutilsFileError: The setup.cfg file /usr/local/bin/Python-3.3.3/setup.cfg does not exist.
  • user3227889
    user3227889 over 10 years
    I spun up a new VPS and did everything from scratch but I still have the same problem!!!
  • yuvi
    yuvi over 10 years
    I have no idea. Sorry.
  • user3227889
    user3227889 over 10 years
    OK so the issue is that it wont pick up models when they are in a 'models' folder! any idea why?
  • user3227889
    user3227889 over 10 years
    I've just given up and kept all models is models.py cos this is too much hasslet - thanks for help anyways.
  • yuvi
    yuvi over 10 years
    I'm sorry, I have misled you. I didn't notice the problem was with trying to use models as a folder. You're original thought was right, though you probably did something wrong there. The edit to my answer now includes a tested working solution. Make sure it looks like that and everything will work