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

Enumerate and remove DEFAULT constraints #38

Open
wants to merge 1 commit into
base: azure-1.8
Choose a base branch
from

Conversation

bobince
Copy link

@bobince bobince commented Aug 4, 2015

SQL Server implements in-database DEFAULT conditions as constraints, and requires that they be explicitly dropped before allowing the related column to be removed. Extend introspection to detect these constraints and return them as a new constraint type. Make the schema editor read and remove these constraints when removing a column.

Django doesn't generally use in-database DEFAULTs, but they can exist in legacy databases other tools have touched. For example: old versions of South would accidentally leave defaults in place after a migration to make a nullable column non-nullable. This can cause unexpected migration failures trying to delete the column later on.

SQL Server implements in-database DEFAULT conditions as constraints, and requires that they be explicitly dropped before allowing the related column to be removed. Extend introspection to detect these constraints and return them as a new
constraint type. Make the schema editor read and remove these constraints when removing a column.

Django doesn't generally use in-database DEFAULTs, but they can exist in legacy databases other tools have touched (for example old versions of South would accidentally leave defaults in place after a migration to make a nullable column
non-nullable).
@shaib
Copy link

shaib commented Aug 4, 2015

FYI, there are discussions going on now, on the django-developers mailing list, looking to add use of database defaults to Django. I would take a good look at the suggestions there to make sure you aren't going against the direction of evolution of Django.
https://groups.google.com/d/topic/django-developers/3mcro17Gb40/discussion

ghost pushed a commit to 3YOURMIND/django-pyodbc-azure that referenced this pull request Mar 28, 2019
Hand merge of michiya#38 :

When removing columns make sure we remove defaults (which Microsoft has
deemed to be constraints).
davidjb pushed a commit to davidjb/django-mssql-backend that referenced this pull request Nov 13, 2020
@sparrowt
Copy link

sparrowt commented Sep 18, 2023

FWIW db_default is now a thing https://code.djangoproject.com/ticket/470#comment:69 (https://docs.djangoproject.com/en/dev/ref/models/fields/#db-default) though not yet released - presumably coming in Django 5.x

Something like this PR may need to be done over on https://github.com/microsoft/mssql-django/ to support this
EDIT: filed microsoft/mssql-django#302

@sparrowt
Copy link

sparrowt commented Sep 6, 2024

Seems this has now been done in microsoft/mssql-django#313

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.

3 participants