jazzband / django-oauth-toolkit

OAuth2 goodies for the Djangonauts!
https://django-oauth-toolkit.readthedocs.io
Other
3.16k stars 794 forks source link

3.0.0 custom model migration non-unique error #1489

Closed n2ygk closed 2 months ago

n2ygk commented 2 months ago

Describe the bug

Not sure this is a DOT error or something else. I will investigate now but wanted to get this out there as soon as possible:

I upgraded to DOT 3.0.0 in my training app which has a custom AccessToken model and the migration failed on non-uniqueness:

(venv) django-jsonapi-training$ ./manage.py showmigrations
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
myapp
 [X] 0001_initial
 [X] 0002_auto_20181019_1821
 [X] 0003_unique_term_identifier
 [X] 0004_instructor
 [X] 0005_auto_20181205_1532
 [X] 0006_person
 [X] 0007_auto_20181206_1727
 [X] 0008_auto_20210414_2018
 [X] 0009_nonmodel
 [X] 0010_grade_student_grade_person
oauth
 [X] 0001_initial
 [X] 0002_auto_20200413_1746
 [X] 0003_alter_myaccesstoken_user_and_more
 [X] 0004_myapplication_allowed_origins_and_more
oauth2_provider
 [X] 0001_initial
 [X] 0002_auto_20190406_1805
 [X] 0003_auto_20201211_1314
 [X] 0004_auto_20200902_2022
 [X] 0005_auto_20211222_2352
 [X] 0006_alter_application_client_secret
 [X] 0007_application_post_logout_redirect_uris
 [X] 0008_alter_accesstoken_token
 [X] 0009_add_hash_client_secret
 [X] 0010_application_allowed_origins
 [ ] 0011_refreshtoken_token_family
 [ ] 0012_add_token_checksum
sessions
 [X] 0001_initial
(venv) django-jsonapi-training$ ./manage.py migrate
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, myapp, oauth, oauth2_provider, sessions
Running migrations:
  Applying oauth2_provider.0011_refreshtoken_token_family... OK
  Applying oauth2_provider.0012_add_token_checksum... OK
(venv) django-jsonapi-training$ ./manage.py showmigrations
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
myapp
 [X] 0001_initial
 [X] 0002_auto_20181019_1821
 [X] 0003_unique_term_identifier
 [X] 0004_instructor
 [X] 0005_auto_20181205_1532
 [X] 0006_person
 [X] 0007_auto_20181206_1727
 [X] 0008_auto_20210414_2018
 [X] 0009_nonmodel
 [X] 0010_grade_student_grade_person
oauth
 [X] 0001_initial
 [X] 0002_auto_20200413_1746
 [X] 0003_alter_myaccesstoken_user_and_more
 [X] 0004_myapplication_allowed_origins_and_more
oauth2_provider
 [X] 0001_initial
 [X] 0002_auto_20190406_1805
 [X] 0003_auto_20201211_1314
 [X] 0004_auto_20200902_2022
 [X] 0005_auto_20211222_2352
 [X] 0006_alter_application_client_secret
 [X] 0007_application_post_logout_redirect_uris
 [X] 0008_alter_accesstoken_token
 [X] 0009_add_hash_client_secret
 [X] 0010_application_allowed_origins
 [X] 0011_refreshtoken_token_family
 [X] 0012_add_token_checksum
sessions
 [X] 0001_initial
(venv) django-jsonapi-training$ ./manage.py makemigrations
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
Migrations for 'oauth':
  oauth/migrations/0005_myaccesstoken_token_checksum_and_more.py
    - Add field token_checksum to myaccesstoken
    - Add field token_family to myrefreshtoken
    - Alter field token on myaccesstoken
(venv) django-jsonapi-training$ ./manage.py showmigrations
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
myapp
 [X] 0001_initial
 [X] 0002_auto_20181019_1821
 [X] 0003_unique_term_identifier
 [X] 0004_instructor
 [X] 0005_auto_20181205_1532
 [X] 0006_person
 [X] 0007_auto_20181206_1727
 [X] 0008_auto_20210414_2018
 [X] 0009_nonmodel
 [X] 0010_grade_student_grade_person
oauth
 [X] 0001_initial
 [X] 0002_auto_20200413_1746
 [X] 0003_alter_myaccesstoken_user_and_more
 [X] 0004_myapplication_allowed_origins_and_more
 [ ] 0005_myaccesstoken_token_checksum_and_more
oauth2_provider
 [X] 0001_initial
 [X] 0002_auto_20190406_1805
 [X] 0003_auto_20201211_1314
 [X] 0004_auto_20200902_2022
 [X] 0005_auto_20211222_2352
 [X] 0006_alter_application_client_secret
 [X] 0007_application_post_logout_redirect_uris
 [X] 0008_alter_accesstoken_token
 [X] 0009_add_hash_client_secret
 [X] 0010_application_allowed_origins
 [X] 0011_refreshtoken_token_family
 [X] 0012_add_token_checksum
sessions
 [X] 0001_initial
(venv) django-jsonapi-training$ ./manage.py migrate
***WARNING: Including relationship patterns. This will break the spectactular command.
Set SPECTACULAR=true in the environment to use the spectacular command.
Set SPECTACULAR=false or missing from the environment to run the server.
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, myapp, oauth, oauth2_provider, sessions
Running migrations:
  Applying oauth.0005_myaccesstoken_token_checksum_and_more...Traceback (most recent call last):
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.IntegrityError: UNIQUE constraint failed: new__oauth_myaccesstoken.token_checksum

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

Traceback (most recent call last):
  File "/Users/ac45/src/django-jsonapi-training/./manage.py", line 26, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
            ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/migrations/operations/fields.py", line 108, in database_forwards
    schema_editor.add_field(
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/sqlite3/schema.py", line 318, in add_field
    self._remake_table(model, create_field=field)
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/sqlite3/schema.py", line 248, in _remake_table
    self.execute(
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/base/schema.py", line 202, in execute
    cursor.execute(sql, params)
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 122, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ac45/src/django-jsonapi-training/venv/lib/python3.12/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: UNIQUE constraint failed: new__oauth_myaccesstoken.token_checksum

To Reproduce

Expected behavior

Version

Additional context

I suspect this may have to do with the migration not creating checksums for old access tokens.