anexia-it / django-rest-passwordreset

An extension of django rest framework, providing a configurable password reset strategy
BSD 3-Clause "New" or "Revised" License
419 stars 148 forks source link

[BUG] Unable to create or change a table without a primary key on migration #149

Open mathijsfr opened 2 years ago

mathijsfr commented 2 years ago

After installing django-rest-passwordreset==1.2.0 with django version 3.2 I added the following to my installed apps:

 INSTALLED_APPS = [
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth', <------
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework', <-----
    'rest_framework_simplejwt.token_blacklist',
    'django_elasticsearch_dsl',
    'django_rest_passwordreset' <---
]

After this I ran python manage.py migrate and I get the following error:

Running migrations:
  Applying django_rest_passwordreset.0001_initial... OK
  Applying django_rest_passwordreset.0002_pk_migration...Traceback (most recent call last):
  .
  .
  .
pymysql.err.OperationalError: (3750, "Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message.
Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.")

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

Traceback (most recent call last):
 .
 .
 .
django.db.utils.OperationalError: (3750, "Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting.")

Looking at the code makes me think the order is wrong in: django-rest-passwordreset/django_rest_passwordreset/migrations/0002_pk_migration.py

First add Id field with primary key before removing the existing primary key

williamweber2 commented 2 years ago

I had this issue Yesterday and I have a suggestion that worked for me:

Go to the migrations file of your package, and add the following:

def get_migrations_for_django_21_and_newer(): return [ # this will enable you to change the table without primary keys, only for the migration db session migrations.RunSQL("SET SESSION sql_require_primary_key=0;"),

remove primary key information from 'key' field

    migrations.AlterField(
        model_name='resetpasswordtoken',
        name='key’,…….