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

2

4

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?

Gab answered 23/1, 2014 at 13:19 Comment(4)
Please edit your original post and add your comment as part of it, it's unreadable like thisBorsch
I've added it to the original post now, any more info needed?Gab
See my other comment pleaseBorsch
Please see my edit for updated solution.Borsch
C
10

Run the following commands

python manage.py makemigrations yourappname

python manage.py migrate

Note it works on django 1.7 version for me.

Coussoule answered 29/11, 2014 at 16:57 Comment(0)
B
8

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!

Borsch answered 23/1, 2014 at 14:54 Comment(20)
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.Gab
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 yetBorsch
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.Gab
I didn't expect it to change anything, I just mentioned it.Borsch
Cool, I have now added an example of one of the models to the description of the issue above.Gab
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 differentBorsch
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.Gab
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 southBorsch
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 DjangoGab
That's probably the problem. Try a fresh install inside an isolated virtualenv, that might do the trickBorsch
I managed to make a fresh project (it just needed django-admin startproject mytestapp - no .py). I have the same issue with the fresh projectGab
Django. Try re-installing djangoBorsch
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.Gab
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...Borsch
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.Gab
I spun up a new VPS and did everything from scratch but I still have the same problem!!!Gab
I have no idea. Sorry.Borsch
OK so the issue is that it wont pick up models when they are in a 'models' folder! any idea why?Gab
I've just given up and kept all models is models.py cos this is too much hasslet - thanks for help anyways.Gab
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 workBorsch

© 2022 - 2024 — McMap. All rights reserved.