Django:创建表失败

3

如果旧表被删除,我不能更改我的模型或创建新的表。我正在使用south,当我向我的模型添加一个新的模型并创建一个新的模型时,我使用了

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)

其实没有改变和创建任何内容,因为当我访问管理员页面时,页面上写着表格不存在。所以我决定删除我的数据库并新建一个,但也失败了:

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个回答

3

这个命令是错误的:

python manage.py schemamigration south --initial

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

你需要为自己的应用程序创建迁移:

python manage.py schemamigration logins --initial

然后,我建议重新安装South,以防在创建那个迁移时出现错误:

pip uninstall south && pip install south

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

最后应用迁移:

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


它现在已经删除了,但是在安装过程中最后两行是:创建 /Library/Python/2.7/site-packages/south错误:无法创建 '/Library/Python/2.7/site-packages/south':权限被拒绝又出错了 :( - lulu
@yudasinal 这是因为你只在卸载时使用了 sudo,而 && 只是用于连接命令,如果你想的话可以分别运行它们:sudo pip uninstall south 然后 sudo pip install south - Adrián
哇,谢谢!安装成功了,但是迁移仍然不起作用!哈哈 它给我同样的错误: django.db.utils.OperationalError: no such table: south_migrationhistory - lulu
另外,请确保同步south表,只需同步所有内容:python manage.py syncdb - Adrián
所以我首先从/migrations文件夹中删除了所有文件,然后运行了python manage.py syncdb,接着运行了python manage.py schemamigration logins --initial,最后运行了python migrate south,但是它给了我同样的错误提示:django.db.utils.OperationalError: no such table: south_migrationhistory,请问这是什么问题? - lulu
显示剩余27条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接