3

I can't change my models and create new tables if the old ones are deleted. I am using south and when I just added a new model to my models and created a new one, I used

python manage.py migrate logins --fake

Running migrations for logins:
- Nothing to migrate.
 - Loading initial data for logins.
Installed 0 object(s) from 0 fixture(s)
Liubous-MacBook-Pro:Django_project_for_EGG yudasinal1$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.admin
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.messages
 > django.contrib.staticfiles

Not synced (use migrations):
 - logins
 - south
(use ./manage.py migrate to migrate these)

Then I said:

python manage.py migrate logins 
Running migrations for logins:
- Nothing to migrate.
 - Loading initial data for logins.
Installed 0 object(s) from 0 fixture(s)

Nothing was actually changed and created, as when I accessed admin, it was written, that the table does not exist. So I decided to delete my database and create a new one, that failed as well:

python manage.py sql logins 

BEGIN;
CREATE TABLE "logins_department" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(200) NOT NULL
)
;
CREATE TABLE "logins_game" (
    "id" integer NOT NULL PRIMARY KEY,
    "name_of_the_game" varchar(200) NOT NULL
)
;
CREATE TABLE "logins_info_game" (
    "id" integer NOT NULL PRIMARY KEY,
    "info_id" integer NOT NULL,
    "game_id" integer NOT NULL REFERENCES "logins_game" ("id"),
    UNIQUE ("info_id", "game_id")
)
;
CREATE TABLE "logins_info_department" (
    "id" integer NOT NULL PRIMARY KEY,
    "info_id" integer NOT NULL,
    "department_id" integer NOT NULL REFERENCES "logins_department" ("id"),
    UNIQUE ("info_id", "department_id")
)
;
CREATE TABLE "logins_info" (
    "id" integer NOT NULL PRIMARY KEY,
    "organization_name" varchar(200) NOT NULL,
    "user_name" varchar(200) NOT NULL,
    "password" varchar(200) NOT NULL
)
;
CREATE TABLE "logins_customuser_department" (
    "id" integer NOT NULL PRIMARY KEY,
    "customuser_id" integer NOT NULL,
    "department_id" integer NOT NULL REFERENCES "logins_department" ("id"),
    UNIQUE ("customuser_id", "department_id")
)
;
CREATE TABLE "logins_customuser_game" (
    "id" integer NOT NULL PRIMARY KEY,
    "customuser_id" integer NOT NULL,
    "game_id" integer NOT NULL REFERENCES "logins_game" ("id"),
    UNIQUE ("customuser_id", "game_id")
)
;
CREATE TABLE "logins_customuser" (
    "user_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "auth_user" ("id")
)
;

COMMIT;


python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > django.contrib.admin
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.messages
 > django.contrib.staticfiles

Not synced (use migrations):
 - logins
 - south
(use ./manage.py migrate to migrate these)
Liubous-MacBook-Pro:Django_project_for_EGG yudasinal1$ python manage.py schemamigration south --initial
 + Added model south.MigrationHistory
Created 0003_initial.py. You can now apply this migration with: ./manage.py migrate south

I migrated them(error again):

python manage.py migrate south
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/South-0.8-py2.7.egg/south/management/commands/migrate.py", line 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/South-0.8-py2.7.egg/south/migration/__init__.py", line 200, in migrate_app
    applied_all = check_migration_histories(applied_all, delete_ghosts, ignore_ghosts)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/South-0.8-py2.7.egg/south/migration/__init__.py", line 79, in check_migration_histories
    for h in histories:
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 781, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 53,
return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 450, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: south_migrationhistory

And so none of the tables were actually created.

Here are my models:

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


class Department(models.Model):
    name = models.CharField(max_length=200)
    def __unicode__(self):  
        return self.name

class Game(models.Model):
    name_of_the_game = models.CharField(max_length=200)
    def __unicode__(self):  
        return self.name_of_the_game

class Info(models.Model):
    organization_name = models.CharField(max_length=200)
    user_name = models.CharField(max_length=200)
    password = models.CharField(max_length=200)
    game = models.ManyToManyField(Game)
    department = models.ManyToManyField(Department)
    def __unicode__(self):
         return self.organization_name+ ': '+ 'user name: ' +self.user_name+ ', '+ 'password: ' + self.password


class CustomUser(User):
    department = models.ManyToManyField(Department)
    game = models.ManyToManyField(Game)
    objects = UserManager()
madth3
  • 7,275
  • 12
  • 50
  • 74
lulu
  • 261
  • 1
  • 4
  • 16

1 Answers1

3

This command is wrong:

python manage.py schemamigration south --initial

schemamigration creates files that describe the migration. South already ships its own migration files with the release.

What you need is to create the migrations for your own app:

python manage.py schemamigration logins --initial

Then, I would reinstall south, just it case it broke when you created that migration:

pip uninstall south && pip install south

EDIT A pip uninstall doesn't remove that migration so you need to delete those files manually: rm -rf /<OS dependent>/python2.7/site-packages/south

Finally apply the migrations:

python manage.py syncdb && python manage.py migrate

Adrián
  • 6,135
  • 1
  • 27
  • 49
  • thanks for your reply! it gave me whole bunch of errors though :( after this command pip uninstall south && pip install south it gave a big error the end line of which is this: OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/South-0.8-py2.7.egg/EGG-INFO/.DS_Store' Storing complete log in /Users/yudasinal1/Library/Logs/pip.log and when I ran migrate: django.db.utils.OperationalError: no such table: south_migrationhistory what do I do with it? – lulu Dec 03 '13 at 14:22
  • @yudasinal you are probably using a system wide pip, try the 'pip' command again with `sudo`. `sudo pip uninstall south && sudo pip install south` – Adrián Dec 03 '13 at 14:27
  • it did delete it now, but then during the installation last two lines were: creating /Library/Python/2.7/site-packages/south error: could not create '/Library/Python/2.7/site-packages/south': Permission denied error again :( – lulu Dec 03 '13 at 14:30
  • @yudasinal that's because you only used `sudo` with the uninstall, the `&&` is just for concatenating commands, you can run then separately if you want: `sudo pip uninstall south` and then `sudo pip install south` – Adrián Dec 03 '13 at 14:32
  • wow, thanks! so the install worked, but the migrations still don't work! haha It gives me the same error: django.db.utils.OperationalError: no such table: south_migrationhistory – lulu Dec 03 '13 at 14:39
  • @yudasinal this `django.db.utils.OperationalError: no such table: south_migrationhistory` ? What do you have under `logins/migrations` ? There should be one `0000_initial.py` only (besides the `__init__`) – Adrián Dec 03 '13 at 14:40
  • I actually have 4 initial.py files: 0001_initial.py, 0002_initial.py, 0003_initial.py, 0004_initial.py and __init__ itself, but no 0000 file – lulu Dec 03 '13 at 14:43
  • @yudasinal ok, delete them all, and run `python manage.py schemamigration logins --initial` again, so that it creates the first one – Adrián Dec 03 '13 at 14:45
  • Also, make sure to `syncdb` the south table, simply `syncdb` everything: `python manage.py syncdb` – Adrián Dec 03 '13 at 14:46
  • so I deleted all the files from the /migrations folder first, then I ran python manage.py syncdb, and then python manage.py schemamigration logins --initial, and then python migrate south and it gave me the same error: django.db.utils.OperationalError: no such table: south_migrationhistory what is wrong with it? – lulu Dec 03 '13 at 14:49
  • @yudasinal `syncdb` should be creating the table for south, the error you are getting is because `migrate` needs that table created, it logs the migrations in the table so that it's possible to run only the necessary migrations whenever `migrate` is ran. Therefore, the problem must be in your database config, or in how you're running syncdb. Could you post your `DATABASES` settings? – Adrián Dec 03 '13 at 14:57
  • thanks for helping out! I am quite stuck and new to django here are my Databases settings: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } – lulu Dec 03 '13 at 14:59
  • @yudasinal ok the defaults, so those are correct. When you run `python manage.py syncdb`, `south` should be listed under "Synced:". If it's not, then your south installation must be broken somewhere. – Adrián Dec 03 '13 at 15:03
  • Good news, I've replicated your steps in a test environment and reinstalling south doesn't remove the migration that you created – Adrián Dec 03 '13 at 15:07
  • south is indeed showing under Unsynced. Could you please tell me how do I fix the broken part then? – lulu Dec 03 '13 at 15:08
  • @yudasinal you'll have to do it manually, run this `rm -rf /usr/lib/python2.7/site-packages/south` You'll need to replace `/usr/lib` with the path of the python libs for your system – Adrián Dec 03 '13 at 15:09
  • ok, and then I have to reinstall it using easy_install again or something like that? – lulu Dec 03 '13 at 15:14
  • @yudasinal yes, http://south.readthedocs.org/en/latest/installation.html#using-easy-install – Adrián Dec 03 '13 at 15:16
  • @yudasinal Note that using a virtualenv is always recommended: [this](http://docs.python-guide.org/en/latest/dev/virtualenvs/) is a good tutorial, that's why you need to run `pip` with `sudo`. – Adrián Dec 03 '13 at 15:20
  • hmmm... So now everything worked fine until migration of the login. I ran: python manage.py schemamigration logins --initial and it gave me: django.db.utils.OperationalError: table "logins_department" already exists – lulu Dec 03 '13 at 15:21
  • @yudasinal you probably created the table running `syncdb` before `schemamigration`. You can delete the whole database, it's the file named `db.sqlite3` next to `manage.py`. Then you'll need to `syncdb` and `migrate` again :( (no `schemamigration`, only `migrate`) – Adrián Dec 03 '13 at 15:27
  • :( that's exactly what i did now: deleted the database, then ran python manage.py syncdb, then ran python manage.py migrate logins and it still gives the same error: django.db.utils.OperationalError: table "logins_department" already exists – lulu Dec 03 '13 at 15:33
  • @yudasinal and is `0001_initial` still the only file in `logins/migrations`? – Adrián Dec 03 '13 at 15:41
  • there's also 0002 there – lulu Dec 03 '13 at 15:42
  • @yudasinal then delete `0002_initial`, you have to be careful not to run `schemamigration --initial` twice for the same app. south executes the migrations in order, that's why it's trying to create the same table twice – Adrián Dec 03 '13 at 15:44
  • ok, I got it. it created everything, I followed your steps again from deleting all the migrations – lulu Dec 03 '13 at 15:45
  • thanks so so much for your help! I think I got how it works now. So basically from now on I never run --initial for this south app, right? I only run the migrate whenever I change my models? Also, how can I know if my manytomany relations were created? Thanks again! – lulu Dec 03 '13 at 15:46
  • @yudasinal django creates a table for each manytomany, it's in the sql code in your question, for instance for the relation in your model Info to Games, the table is "logins_info_game" – Adrián Dec 03 '13 at 15:53
  • @yudasinal and no, don't run `schemamigration` for 3rd party apps, the authors will do it if required and the migration files will be included in the release. – Adrián Dec 03 '13 at 15:55
  • @yudasinal if you make changes to your model, running `migrate` again won't do anything. You'll need to run `schemamigration --auto logins`, but you don't have to do it for each change, you can create a migration that includes several changes – Adrián Dec 03 '13 at 15:57
  • @yudasinal there's a good summary [here](http://south.readthedocs.org/en/latest/tutorial/part1.html#the-first-migration) that includes changes to models – Adrián Dec 03 '13 at 15:57
  • thank you! All of it was really helpful for my understanding! – lulu Dec 03 '13 at 16:04