Monday, July 2, 2018

Django- Change Username field to BigAutoField?

Leave a Comment

I'm designing an Application where username will be an AutoIntegerField and unique.

Here's my model.

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

Initially, I wanted user_id to be a primary_key, but I can't create an AutoField which is not primary_key. As a result, I'd to let go off user_id as primary_key and assigned username as the primary key.

Now, when I run the migrations, it throws an error saying,

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

Complete 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 

1 Answers

Answers 1

I think the issue is that you still have an old index on your username field that clashes with the new type. The db_index=False argument has no effect because primary_key=True always generates an index.

You might be able to solve this by removing primary_key=True, creating a migration, and then re-adding it and creating another migration. Alternatively, you can do it by hand by dropping and re-creating the index. If you want to go down that path, consult this answer.

If your project is still in an early stage and you don't have any valuable data, you can take the easy way out and drop any tables related to your users app or even the complete database, delete all migrations in the users app and create them from scratch.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment