问题
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