我刚刚切换到this post之后使用自定义AUTH_USER_MODEL。
它建议进行以下向前迁移:
def forwards(self, orm):
db.rename_table('auth_user', 'myapp_myuser')
db.rename_table('auth_user_groups', 'myapp_myuser_groups')
db.rename_table('auth_user_user_permissions',
'myapp_myuser_user_permissions')
if not db.dry_run:
# For permissions to work properly after migrating
orm['contenttypes.contenttype'].objects.filter(app_label='auth',
model='user').update(app_label='myapp', model='myuser')
这适用于执行syncdb
的现有数据库,而AUTH_USER_MODEL
仍然是 settings.py auth.User >。这是因为auth_user
表已经创建。
但是,在使用包含AUTH_USER_MODEL = 'myapp.MyUser'
的新 settings.py 的新安装中,运行syncdb
不会创建auth_user
}表。当然,如果我现在使用manage.py migrate myapp
运行myapp的迁移,则会出现以下错误:
Running migrations for myapp:
- Migrating forwards to 0002_custom_auth_user_model.
> myapp:0001_initial
> myapp:0002_custom_auth_user_model
FATAL ERROR - The following SQL query failed: RENAME TABLE `auth_user` TO `myapp_myuser`;
The error was: (1017, "Can't find file: '.\\mydb\\auth_user.frm' (errno: 2)")
! Error found during real run of migration! Aborting.
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: = RENAME TABLE `myapp_myuser` TO `auth_user`; []
= RENAME TABLE `myapp_myuser_groups` TO `auth_user_groups`; []
= RENAME TABLE `myapp_myuser_user_permissions` TO `auth_user_user_permissions`; []
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: myapp:0002_custom_auth_user_model
DatabaseError: (1017, "Can't find file: '.\\mydb\\auth_user.frm' (errno: 2)")
我想在条件中包装迁移以检查auth_user
表是否已经存在,但是South似乎不支持获取数据库表,我也不确定这是最好的想法。< / p>
如何修复迁移,以便它适用于新安装和现有安装?