5

I have a Django app that has CustomUser. My model looks something like

class CustomUser(AbstractBaseUser):
    def get_short_name(self):
        pass

    def get_full_name(self):
        pass

    firstName = models.CharField(max_length=300)
    middleName = models.CharField(max_length=300, blank=True)
    lastName = models.CharField(max_length=300, blank=True)
    username = models.CharField(unique=True, max_length=50)
    businessName = models.CharField(max_length=500, default=None)
    mobileNumber = models.CharField(max_length=20)
    contactNumber = models.CharField(max_length=20)
    addressLine1 = models.CharField(max_length=300)
    addressLine2 = models.CharField(max_length=300)
    city = models.CharField(max_length=300)
    state = models.CharField(max_length=300)
    role = models.CharField(max_length=20)
    email_id = models.CharField(max_length=300, unique=True)
    aadharNumber = models.BigIntegerField(default=0)
    panNumber = models.CharField(max_length=20, default=None)
    registrationDate = models.BigIntegerField(default=0)
    bankDetail = models.ManyToManyField('BankDetail', related_name="bankDetail")
    dateCreated = models.DateTimeField(auto_now_add=True)
    dateModified = models.DateTimeField(auto_now=True)
    objects = AccountManager()

    USERNAME_FIELD = 'email_id'
    REQUIRED_FIELDS = ['username'] 

I was following the example in this blog https://afropolymath.svbtle.com/authentication-using-django-rest-framework to implement User authentication.

I get the following error when I run makemigrations I did look at a few solutions on StackOverflow but those don't seem to solve my problem.

Django error message "Add a related_name argument to the definition"

AlterField on auto generated _ptr field in migration causes FieldError

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 177, in handle
    migration_name=self.migration_name,
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 47, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/autodetector.py", line 133, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/state.py", line 222, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/state.py", line 288, in __init__
    self.render_multiple(list(models.values()) + self.real_models)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/state.py", line 323, in render_multiple
    model.render(self)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/state.py", line 626, in render
    body,
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 259, in __new__
    base.__name__,
django.core.exceptions.FieldError: Auto-generated field 'user_ptr' in class 'CustomUser' for parent_link to base class 'User' clashes with declared field of the same name.
customer ID<django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x106341a58>

What do I need to do to get rid of this error and proceed with a successful migration?

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
LeoNeo
  • 739
  • 1
  • 9
  • 28
  • strange, shouldn't happen. Where does `AbstractBaseUser` come from ? – Linovia Oct 17 '17 at 08:35
  • it's coming from django.contrib.auth.models. But I removed my migrations and my database and then re ran makemigrations and migrate and that worked. So I believe it had something to do with the sequence of my changes in the migrations that was causing this? – LeoNeo Oct 20 '17 at 03:48
  • 1
    Might be explained if you generated migrations before changing the default user in settings. – Linovia Oct 20 '17 at 08:32
  • yes I did. But why should that have an issue with this? I want to understand what went wrong even though I was able to make it work. – LeoNeo Oct 20 '17 at 09:26
  • Because migration builds the models from what is declared in the migrations so it has a FK to/from auth.User and then it's supposed to be to something else. Code doesn't have this issue although you'll get DB integrity issues instead. – Linovia Oct 20 '17 at 10:17
  • Same had to delete db and rerun migrations. Mine came from renaming my base model I was using – Tjorriemorrie Nov 12 '18 at 07:46

1 Answers1

1

Delete your migrations . Then run makemigrations command.

Bilal Tahir
  • 45
  • 1
  • 7
  • 2
    Sometimes you get so far above the basics, you just forget to do some simple things. Thanks for the reminder. :) – John Q Oct 05 '19 at 17:42
  • 10
    Yeah, but how to fix it for production mode where you never want to delete your migrations? – ElectRocnic Aug 30 '20 at 20:50