【问题标题】:Auto-generated field 'user_ptr' clashes with declared field of the same name自动生成的字段“user_ptr”与已声明的同名字段冲突
【发布时间】:2018-03-28 09:04:58
【问题描述】:

我有一个具有 CustomUser 的 Django 应用程序。我的模型看起来像

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'] 

我是按照这个博客https://afropolymath.svbtle.com/authentication-using-django-rest-framework中的例子来实现用户认证的。

运行makemigrations 时出现以下错误 我确实在 StackOverflow 上查看了一些解决方案,但似乎并没有解决我的问题。

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>

我需要做什么才能消除此错误并继续成功迁移?

【问题讨论】:

  • 奇怪,不应该发生。 AbstractBaseUser 来自哪里?
  • 它来自 django.contrib.auth.models。但是我删除了我的迁移和我的数据库,然后重新运行 makemigrations 和 migrate 并且有效。所以我认为这与我在迁移中的更改顺序有关吗?
  • 如果您在更改设置中的默认用户之前生成了迁移,可能会得到解释。
  • 是的,我做到了。但是为什么这会有问题呢?即使我能够让它工作,我也想了解出了什么问题。
  • 因为迁移从迁移中声明的内容构建模型,因此它具有与 auth.User 之间的 FK,然后它应该是其他东西。代码没有这个问题,尽管您会遇到数据库完整性问题。

标签: django python-3.x django-rest-framework django-authentication


【解决方案1】:

删除您的迁移。 然后运行 ​​makemigrations 命令。

【讨论】:

  • 有时你已经超越了基础知识,只是忘记了做一些简单的事情。感谢您的提醒。 :)
  • 是的,但是如何在您不想删除迁移的生产模式下修复它?
猜你喜欢
  • 2017-09-13
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多