【问题标题】:Django- Change Username field to BigAutoField?Django-将用户名字段更改为 BigAutoField?
【发布时间】:2018-12-01 15:20:57
【问题描述】:

我正在设计一个应用程序,其中用户名将是 AutoIntegerField 并且是唯一的。

这是我的模型。

class ModelA(models.Model):
    username = models.BigAutoField(primary_key=True, db_index=False)
    user_id = models.UUIDField(default=uuid.uuid4, unique=True,
                               editable=False)

最初,我希望 user_id 是 primary_key,但我无法创建不是 primary_key 的 AutoField。因此,我会放弃 user_id 作为主键,并指定 username 作为主键。

现在,当我运行迁移时,它会抛出一个错误提示,

django.db.utils.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint

完成 StackTrace:-

  Applying users.0005_auto_20180626_0914...Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
    old_db_params, new_db_params, strict)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
    new_db_params, strict,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 684, in _alter_field
    params,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: operator class "varchar_pattern_ops" does not accept data type bigint

【问题讨论】:

  • 请向我们展示完整的堆栈跟踪
  • @DanielHepper - 使用 Traceback 更新问题。请看一看。

标签: python django django-models


【解决方案1】:

我认为问题在于您的 username 字段上仍有一个旧索引与新类型冲突。 db_index=False 参数无效,因为 primary_key=True 总是生成索引。

您可以通过删除 primary_key=True、创建迁移,然后重新添加它并创建另一个迁移来解决此问题。或者,您可以通过删除并重新创建索引来手动完成。如果你想走这条路,请咨询this answer

如果您的项目仍处于早期阶段并且您没有任何有价值的数据,您可以采取简单的方法并删除与您的 users 应用程序甚至整个数据库相关的任何表,删除所有迁移users 应用程序并从头开始创建它们。

【讨论】:

  • 1) 我(后来)意识到 PK 是自索引的,因此删除了 db_index=True,但没有用。 2) 一个模型不能有多个 AutoField。因此,无法从用户名字段中删除 primary_key=True3) 我尝试了最后一个建议,它奏效了。但是,还有另一个与之相关的问题。我会为它打开另一个线程。
  • 你能看看这个链接吗? stackoverflow.com/questions/51042246/…
【解决方案2】:

在我的情况下,我将 django 连接到 localhost pgadmin 的 postgres 首先删除了除默认迁移之外的所有迁移以及pycache中的所有迁移 然后只需在终端中运行 python manage.py makemigrations 和 python manage.py migrate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多