2

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?

Community
  • 1
  • 1
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • are the two other tables in question part of your own code or does it come from an app? – e4c5 Jun 04 '16 at 08:05
  • I didn't create those two other tables. – Saqib Ali Jun 04 '16 at 08:39
  • Is the profile table part of your code? Can you name the app you use for profiles? – e4c5 Jun 04 '16 at 09:19
  • The app name: `profiles`. Class name `Profile`. Here is what gets created in the Database: https://drive.google.com/file/d/0B1UFylbhbG2eTmh4TWFuNnBCRDg/view?usp=sharing – Saqib Ali Jun 05 '16 at 03:01
  • Sorry for late reply. You will have to change all the models at once. Might even need to edit the migrations file. – e4c5 Jun 07 '16 at 10:06

0 Answers0