如何在保留现有数据的同时修补用户模型?

时间:2022-05-15 06:07:07

I have a big Django application, currently trying to upgrade from 1.6 to 1.7 (there's been attempts to upgrade straight to 1.11, but it was too much trouble, so my plan is to do it one minor at a time).

我有一个很大的Django应用程序,目前正试图从1.6升级到1.7(已经尝试直接升级到1.11,但这太麻烦了,所以我的计划是一次一个小的)。

I'm following the Upgrade from South instructions, and deleted all previous migrations, but I can't get makemigrations to work. The current problem is that the auth.User model has been patched to include two new fields:

我正在遵循South的升级说明,并删除了之前的所有迁移,但我无法让makemigrations工作。当前的问题是auth.User模型已经修补以包含两个新字段:

User.add_to_class('profile',
                  models.ForeignKey('access.Profile', null=True, blank=True,
                                    related_name='user_foreignkey'))
User.add_to_class('profiles', 
                  models.ManyToManyField('access.Profile', null=True,
                                         blank=True))

This patch was made in a separate app. If I just leave it where it is, I get the following error when running python manage.py makemigrations:

这个补丁是在一个单独的应用程序中制作如果我把它放在原来的位置,运行python manage.py makemigrations时会出现以下错误:

ValueError: Lookup failed for model referenced by field auth.User.profiles:
access.Profiles

I tried moving the add_to_class calls to the same file where Profile is defined, (after the definition), but got the same error. I also tried changing the syntax from 'access.Profile' to Profile, to no effect. Is there something else that could make this work?

我尝试将add_to_class调用移动到定义了Profile的同一文件中(在定义之后),但是得到了相同的错误。我也尝试将语法从'access.Profile'更改为Profile,无效。还有其他什么可以使这项工作?

If not, since I'm adding fields to the model, I figure the correct approach would be extend the AbstractUser model, as suggested by this guide. The problem with this is that the new initial migration will create a table access_user instead of using the existing auth_user. Would it be safe to simply rename auth_user to access_user and fake the migration?

如果没有,因为我正在向模型中添加字段,我认为正确的方法是扩展AbstractUser模型,如本指南所示。这样做的问题是新的初始迁移将创建一个表access_user,而不是使用现有的auth_user。简单地将auth_user重命名为access_user并伪造迁移是否安全?

Any other suggestions on how to overcome this with the least refactoring possible (management always thinks there are more urgent things than the upgrade) are welcome.

关于如何以最少的重构来解决这个问题的任何其他建议(管理层总是认为比升级更紧急的事情)是受欢迎的。

1 个解决方案

#1


0  

tl, dr; I removed auth migrations and ran makemigrations again, that seems to have solved the problem.

tl,博士;我删除了auth迁移并再次运行makemigrations,这似乎已经解决了问题。

I had decided to give option 2 a try. I removed the add_to_class patches, created a new class User(AbstractUser) with the new fields, removed all the existing migrations and ran makemigrations again. I got the exact same error, which made no sense because there was no longer any code relating auth.User to Profile, so I decided to investigate auth migrations. I ran

我决定尝试选项2。我删除了add_to_class补丁,创建了一个带有新字段的新类User(AbstractUser),删除了所有现有的迁移并再次运行makemigrations。我得到了完全相同的错误,这没有任何意义,因为不再有任何代码将auth.User与Profile相关联,所以我决定调查auth迁移。我跑了

$ python migrate --list settings=settings.mysettings

and it showed a suspicious migration for auth, with yesterday's date, probably when I first tried to make migrations.

并且它显示了一个可疑的身份验证迁移,在昨天的日期,可能是我第一次尝试进行迁移时。

Tutorials on "how to reset all migrations" do not mention third-party installed apps, but thanks to this thread I knew where Django stored the auth migrations, so I removed both of them (the initial and the suspicious one) and reordered the INSTALLED_APPS so that access is now before django.contrib.auth. Then ran makemigrations again, and it got through access and auth migrations successfully.

关于“如何重置所有迁移”的教程没有提到第三方安装的应用程序,但是由于这个线程,我知道Django存储了auth迁移的位置,所以我删除了它们(初始和可疑的)并重新排序了INSTALLED_APPS所以现在访问django.contrib.auth之前。然后再次运行makemigrations,它成功通过访问和身份验证迁移。

I'm now getting other problems with migrations in other apps, but they don't seem to be related with this.

我现在在其他应用程序中遇到其他迁移问题,但它们似乎与此无关。

It might be nice to have a more intuitive way to reset all existing migrations, not only those from your repo. But then again, I don't think the add_to_class patches are the proper way to do what we wanted.

以更直观的方式重置所有现有迁移可能会很好,而不仅仅是来自您的仓库的迁移。但话说回来,我认为add_to_class补丁并不是我们想要的正确方法。

#1


0  

tl, dr; I removed auth migrations and ran makemigrations again, that seems to have solved the problem.

tl,博士;我删除了auth迁移并再次运行makemigrations,这似乎已经解决了问题。

I had decided to give option 2 a try. I removed the add_to_class patches, created a new class User(AbstractUser) with the new fields, removed all the existing migrations and ran makemigrations again. I got the exact same error, which made no sense because there was no longer any code relating auth.User to Profile, so I decided to investigate auth migrations. I ran

我决定尝试选项2。我删除了add_to_class补丁,创建了一个带有新字段的新类User(AbstractUser),删除了所有现有的迁移并再次运行makemigrations。我得到了完全相同的错误,这没有任何意义,因为不再有任何代码将auth.User与Profile相关联,所以我决定调查auth迁移。我跑了

$ python migrate --list settings=settings.mysettings

and it showed a suspicious migration for auth, with yesterday's date, probably when I first tried to make migrations.

并且它显示了一个可疑的身份验证迁移,在昨天的日期,可能是我第一次尝试进行迁移时。

Tutorials on "how to reset all migrations" do not mention third-party installed apps, but thanks to this thread I knew where Django stored the auth migrations, so I removed both of them (the initial and the suspicious one) and reordered the INSTALLED_APPS so that access is now before django.contrib.auth. Then ran makemigrations again, and it got through access and auth migrations successfully.

关于“如何重置所有迁移”的教程没有提到第三方安装的应用程序,但是由于这个线程,我知道Django存储了auth迁移的位置,所以我删除了它们(初始和可疑的)并重新排序了INSTALLED_APPS所以现在访问django.contrib.auth之前。然后再次运行makemigrations,它成功通过访问和身份验证迁移。

I'm now getting other problems with migrations in other apps, but they don't seem to be related with this.

我现在在其他应用程序中遇到其他迁移问题,但它们似乎与此无关。

It might be nice to have a more intuitive way to reset all existing migrations, not only those from your repo. But then again, I don't think the add_to_class patches are the proper way to do what we wanted.

以更直观的方式重置所有现有迁移可能会很好,而不仅仅是来自您的仓库的迁移。但话说回来,我认为add_to_class补丁并不是我们想要的正确方法。