Skip to content

Accept deferrable kwarg for schema editor SQL #86

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

Merged
merged 1 commit into from
Jan 11, 2021

Conversation

davidjb
Copy link

@davidjb davidjb commented Nov 13, 2020

Using the current master and trying to run migrations on Django 3.1.4 results in the following error:

(0.102) QUERY = 'CREATE TABLE [wagtailcore_workflowtask] ([id] int IDENTITY (1, 1) NOT NULL PRIMARY KEY, [sort_order] int NULL, [task_id] int NOT NULL, [workflow_id] int NOT NULL)' - PARAMS = (); args=None
  Applying wagtailcore.0047_add_workflow_models...Traceback (most recent call last):
  File "./manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
[...]
  File "lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 808, in database_forwards
    schema_editor.add_constraint(model, self.constraint)
  File "lib/python3.8/site-packages/django/db/backends/base/schema.py", line 360, in add_constraint
    sql = constraint.create_sql(model, self)
  File "lib/python3.8/site-packages/django/db/models/constraints.py", line 118, in create_sql
    return schema_editor._create_unique_sql(
TypeError: _create_unique_sql() got an unexpected keyword argument 'deferrable'

This PR adds the keyword arg to the function definition so it will be accepted. It also adjusts the implementation of this function to match that of the BaseDatabaseSchemaEditor, checking for deferrable support and handling invalid situations. The result is effectively the same, an empty string passed to the Statement() calls.

@honyczek
Copy link

This patch helped me to resolve issue #63

@OskarPersson
Copy link
Collaborator

Please rebase this against master and fix the failing tests on Django 2.2 and 3.0

@davidjb
Copy link
Author

davidjb commented Jan 3, 2021

@OskarPersson sure, can do. Will be into next week some time when I’ll have an environment to work on it

@davidjb davidjb force-pushed the deferrable branch 3 times, most recently from 4883a50 to 6275ca6 Compare January 11, 2021 07:38
This follows Django 3.1.x's lead in adding `deferrable` as a keyword
argument to the `_create_unique_sql` function, following their
implementation in the schema editor object.  Without accepting this
keyword arg, migrations will crash like so:

```
...
  File "lib/python3.8/site-packages/django/db/backends/base/schema.py", line 360, in add_constraint
    sql = constraint.create_sql(model, self)
  File "lib/python3.8/site-packages/django/db/models/constraints.py", line 118, in create_sql
    return schema_editor._create_unique_sql(
TypeError: _create_unique_sql() got an unexpected keyword argument 'deferrable'
```

This also adjusts the implementation to call
`self_deferrable_constraint_sql(...)` with the deferrable arg; in this
backend's case the return value is an empty string (like it is currently
hardcoded).  Essentially it's the same result but allows flexibility if
this backend ever supported deferrable constraints.

Backwards compatibility is maintained by checking for the passing of a
deferrable keyword argument to the create sql function (not provided on
earlier Django versions before 3.1) and that the database backend
supports deferral for unique indexes.
@davidjb
Copy link
Author

davidjb commented Jan 11, 2021

Rebased the PR and tests are now passing on 2.2 and 3.0. Had to add a version number check since the Statement constructor requires deferrable in 3.1 but fails with the kwarg present in earlier versions.

@OskarPersson OskarPersson merged commit 33389b6 into ESSolutions:master Jan 11, 2021
@OskarPersson
Copy link
Collaborator

Thanks!

@HarryPotterDeveloper
Copy link

Thanks it worked for me

@svbfromnl
Copy link

Can you create a new build and update the package at https://pypi.org/project/django-mssql-backend/ ?
Until the package is updated #63 will remain out there in the wild. I just ran into it today.

davidjb added a commit to davidjb/mssql-django that referenced this pull request Mar 3, 2021
This was previously accepted to the original repository in
ESSolutions/django-mssql-backend#86.

Backwards compatibility for Django < 3.1 is maintained by not directly
trying to load the supports_deferrable_unique_constraints via dot
notation, but rather by getattr with a default.
davidjb added a commit to davidjb/mssql-django that referenced this pull request Mar 3, 2021
This was previously accepted to the original repository in
ESSolutions/django-mssql-backend#86.

Backwards compatibility for Django < 3.1 is maintained by not directly
trying to load the supports_deferrable_unique_constraints via dot
notation, but rather by getattr with a default.
davidjb added a commit to davidjb/mssql-django that referenced this pull request Mar 3, 2021
This was previously accepted to the original repository in
ESSolutions/django-mssql-backend#86.

Backwards compatibility for Django < 3.1 is maintained by not directly
trying to load the supports_deferrable_unique_constraints via dot
notation, but rather by getattr with a default.
davidjb added a commit to davidjb/mssql-django that referenced this pull request Mar 3, 2021
This was previously accepted to the original repository in
ESSolutions/django-mssql-backend#86.

Backwards compatibility for Django < 3.1 is maintained by not directly
trying to load the supports_deferrable_unique_constraints via dot
notation, but rather by getattr with a default.
@davidjb
Copy link
Author

davidjb commented Mar 3, 2021

@svbfromnl https://github.com/microsoft/mssql-django features this fix (just not backwards compatible for < Django 3.1 at the moment); installable via PyPI @ https://pypi.org/project/mssql-django/.

I've opened microsoft/mssql-django#9 to re-add support for older Django.

@tlugo-deacero
Copy link

@davidjb Thanks for this fix! However, the current version of mssql-django 1.0a5 does not contain the feature. Could you build the package with it?

@davidjb
Copy link
Author

davidjb commented Mar 11, 2021

@tlugo-deacero As best I can tell it does (https://github.com/microsoft/mssql-django/blob/dev/mssql/schema.py#L704-L706) -- just that it's not backwards compatible with < Django 3.1. I switched my active project over to using mssql-django and my migrations and database ORM run okay without the errors seen here.

Edit: oh, I see what you mean -- the packaged version doesn't have it. In that case, definitely raise an issue over there to ask Microsoft.

Note that I'm not a maintainer of that package, just a contributor -- microsoft/mssql-django#9 (to add Django < 3.1 support as was added here in #86) isn't yet merged so feel free to add a comment or 👍 over there.

@legioz
Copy link

legioz commented May 4, 2021

This issue is stille happening to me, Im using Django 3.1
image

@legioz
Copy link

legioz commented May 4, 2021

@svbfromnl https://github.com/microsoft/mssql-django features this fix (just not backwards compatible for < Django 3.1 at the moment); installable via PyPI @ https://pypi.org/project/mssql-django/.

I've opened microsoft/mssql-django#9 to re-add support for older Django.

The last version is from 2016
The package looks a bit deprecated

@davidjb
Copy link
Author

davidjb commented May 4, 2021

@luizfelipevbll Last version of mssql-django is out from 19 Mar 2021 (https://pypi.org/project/mssql-django/#history) - it's being actively worked on and I use it with Django 3.1 at present.

@legioz
Copy link

legioz commented May 4, 2021

@luizfelipevbll Last version of mssql-django is out from 19 Mar 2021 (https://pypi.org/project/mssql-django/#history) - it's being actively worked on and I use it with Django 3.1 at present.

Shit, I was looking in the wrong pkg django-mssql istead of mssql-django

Is mssql-django 1.0b1 stable for production?
Does it support django 3.2?

@davidjb
Copy link
Author

davidjb commented May 4, 2021

@luizfelipevbll Not with that latest packaged version, no -- doesn't support Django 3.2 and has issues with certain DB/ORM operations (in particular you'd be interested in microsoft/mssql-django#9 which re-adds on aspect of Django 3.1 support). The dev version has these fixes, plus those that fix microsoft/mssql-django#25 for Django 3.2 support. I'm not using Django 3.2 at this point, so can't comment more than I've read on the issue tracker.

Smart-Dev00214 added a commit to Smart-Dev00214/mssql-django that referenced this pull request Feb 20, 2024
This was previously accepted to the original repository in
ESSolutions/django-mssql-backend#86.

Backwards compatibility for Django < 3.1 is maintained by not directly
trying to load the supports_deferrable_unique_constraints via dot
notation, but rather by getattr with a default.
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

Successfully merging this pull request may close these issues.

7 participants