I have a Django project that has many applications. Each application has a models.py file.
I use this strategy to change all models in my project so 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 works well for most of my models. Besides the problem with one model. This is my Profile model, defined below:
class Profile(CachingMixin, AbstractUser): full_name = models.CharField(max_length=254,null=False, blank=False,)
I will add the following line to this model:
id = models.BigIntegerField(primary_key=True)
After that, I successfully run manage.py makemigrations . A new migration file is 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 fix this problem? Obviously, this has something to do with inheritance from AbstractUser . Since 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 these two child tables has a profile_id column that points to the primary key of the parent table. The two columns are INT. Therefore, when I change the parent primary key to BIGINT, it is interrupted. What is the solution here?
source share