Django:创建表失败

时间:2013-12-03 13:45:22

标签: django sqlite django-south database-table

如果旧表格被删除,我无法更改模型并创建新表格。我正在使用南方,当我刚刚为我的模型添加了一个新模型并创建了一个新模型时,我使用了

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)

然后我说:

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)

实际上没有任何内容被更改和创建,因为当我访问admin时,它被写入,该表不存在。所以我决定删除我的数据库并创建一个新数据库,但也失败了:

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

我迁移了它们(再次出错):

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

所以没有实际创建的表。

以下是我的模特:

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()

1 个答案:

答案 0 :(得分:3)

此命令错误:

python manage.py schemamigration south --initial

schemamigration创建描述迁移的文件。 South已经发布了自己的迁移文件。

您需要为自己的应用创建迁移:

python manage.py schemamigration logins --initial

然后,我会重新安装南方,只是它在你创建迁移时破坏了:

pip uninstall south && pip install south

编辑点数卸载不会删除该迁移,因此您需要手动删除这些文件:rm -rf /<OS dependent>/python2.7/site-packages/south

最后应用迁移:

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