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

Migrations "fail" when using MSSQL as database #143

Closed
ivissani opened this issue Aug 4, 2020 · 3 comments
Closed

Migrations "fail" when using MSSQL as database #143

ivissani opened this issue Aug 4, 2020 · 3 comments

Comments

@ivissani
Copy link

ivissani commented Aug 4, 2020

Describe the bug
An exception is thrown when running migrations using MSSQL as database backend. The error reported by SQL Server is the following:

The operation failed because an index or statistics with name 'rest_framework_api_key_apikey_prefix_4e0db5f8_uniq' already exists on table 'rest_framework_api_key_apikey'. (1913) (SQLExecDirectW).

Looking at the code in AbstractAPI I conjecture that the error is produced because of the definition of the id field:

id = models.CharField(max_length=100, unique=True, primary_key=True, editable=False)

My guess is that, for some reason, SQL Server is actually trying to create two times the same index because both primary_key AND unique are set to true. Even though this is probably a bug in django-mssql-backend, I'm reporting this here because it's my understanding that primary_key already implies unique and therefore it should not be necessary to set unique. Am I right?

To Reproduce
Steps to reproduce the behavior:

  1. Set up a django project using django-mssql-backend as database backend and djangorestframework-api-key
  2. Run migrations
  3. See error

Desktop (please complete the following information):

  • OS: Linux and OSX
  • Python Version: 3.7
  • Django Version: 2.2
  • Django Rest Framework APIKey Version: 2.0.0
  • DRF Version: 3.11.0
  • django-mssql-backend Version: 2.8.1
@florimondmanca
Copy link
Owner

Hi,

It's my understanding that primary_key already implies unique and therefore it should not be necessary to set unique. Am I right?

Sounds fair. 🤔 Does removing unique=True allow your migration to pass?

@ivissani
Copy link
Author

ivissani commented Aug 4, 2020

Actually not... and the exception comes from this 0004_prefix_hashed_key migration, so it seems has nothing to do with combining primary_key with unique.
I will do a more thorough research and get back with better insight. At this point it seems to be django-mssql-backend's fault completely.

@ivissani
Copy link
Author

I'm closing this since is not related to this package. The fault was actually an issue in django-mssql-backend: ESSolutions/django-mssql-backend#75

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