Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mathijsfr opened this issue Oct 14, 2021 · 1 comment

Comments

@mathijsfr
Copy link

mathijsfr commented Oct 14, 2021

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
Copy link

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’,…….

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants