Django: How to migrate primary key to BigIntegerField of class that inherits from AbstractUser

。_饼干妹妹 提交于 2019-12-12 02:18:19

问题


I have a Django project that has numerous applications. Each application has a models.py file.

I'm using this strategy to modify all the models in my project such that their primary keys are explicitly defined as id = models.BigIntegerField(primary_key=True) instead of implicitly defined as id = models.IntegerField(primary_key=True)

This strategy is working well for most of my models. Except there is a problem with one model. It is my Profile model defined below:

class Profile(CachingMixin, AbstractUser):
    full_name = models.CharField(max_length=254,null=False, blank=False,)

I add the following line to this model:

id = models.BigIntegerField(primary_key=True)

After that, I successfully run the manage.py makemigrations. A new migration file gets created. However, when I apply this migration, I get the following error:

django.db.utils.OperationalError: (1833, "Cannot change column 'id': used in a foreign key constraint 'profiles_prof_profile_id_59a0cf98572a1aaa_fk_profiles_profile_id' of table 'myapp_db.profiles_profile_groups'")

How can I remedy this problem? Clearly it has something to do with inheriting from AbstractUser. Because Profile inherits from the AbstractUser class, it comes with its own baggage: 2 child tables: profiles_profile_group and profiles_profile_user_permissions. Each of those two child tables have a column profile_id which points back to the parent table's primary key. Those two columns are INT. So when I change the parent's primary key to BIGINT, it breaks. What is the solution here?

来源:https://stackoverflow.com/questions/37627600/django-how-to-migrate-primary-key-to-bigintegerfield-of-class-that-inherits-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!