├── migration_poc ├── __init__.py ├── wsgi.py ├── urls.py └── settings.py ├── migration_poc_app ├── __init__.py ├── migrations │ ├── __init__.py │ ├── 0002_auto_20160114_2114.py │ └── 0001_initial.py ├── tests.py ├── admin.py ├── views.py └── models.py ├── .gitignore ├── requirements.txt ├── manage.py └── README.md /migration_poc/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /migration_poc_app/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /migration_poc_app/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .DS_Store 3 | /venv 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.8.8 2 | psycopg2==2.6.1 3 | -------------------------------------------------------------------------------- /migration_poc_app/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /migration_poc_app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /migration_poc_app/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /migration_poc_app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | class Person(models.Model): 4 | name = models.CharField(max_length=255, db_index=True, blank=True) 5 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "migration_poc.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /migration_poc/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for migration_poc project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "migration_poc.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /migration_poc_app/migrations/0002_auto_20160114_2114.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import unicode_literals 3 | 4 | from django.db import migrations, models 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('migration_poc_app', '0001_initial'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AlterField( 15 | model_name='person', 16 | name='name', 17 | field=models.CharField(db_index=True, unique=True, max_length=255, blank=True), 18 | ), 19 | ] 20 | -------------------------------------------------------------------------------- /migration_poc_app/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from __future__ import unicode_literals 3 | 4 | from django.db import migrations, models 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name='Person', 15 | fields=[ 16 | ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), 17 | ('name', models.CharField(db_index=True, max_length=255, blank=True)), 18 | ], 19 | ), 20 | ] 21 | -------------------------------------------------------------------------------- /migration_poc/urls.py: -------------------------------------------------------------------------------- 1 | """migration_poc URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.8/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 14 | """ 15 | from django.conf.urls import include, url 16 | from django.contrib import admin 17 | 18 | urlpatterns = [ 19 | url(r'^admin/', include(admin.site.urls)), 20 | ] 21 | -------------------------------------------------------------------------------- /migration_poc/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for migration_poc project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.8.8. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.8/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.8/ref/settings/ 11 | """ 12 | 13 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 14 | import os 15 | 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '9+b7tir#gm3ye%c7v(4k=s!$&mi&rhzei!g8sf38ppr2vzyn^3' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = ( 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'migration_poc_app', 41 | ) 42 | 43 | MIDDLEWARE_CLASSES = ( 44 | 'django.contrib.sessions.middleware.SessionMiddleware', 45 | 'django.middleware.common.CommonMiddleware', 46 | 'django.middleware.csrf.CsrfViewMiddleware', 47 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 48 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | 'django.middleware.security.SecurityMiddleware', 52 | ) 53 | 54 | ROOT_URLCONF = 'migration_poc.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'migration_poc.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/1.8/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.postgresql_psycopg2', 81 | 'NAME': 'migration_poc', 82 | 'USER': 'zack', 83 | 'PASSWORD': '', 84 | 'HOST': '', 85 | 'PORT': 5432 86 | } 87 | } 88 | 89 | 90 | # Internationalization 91 | # https://docs.djangoproject.com/en/1.8/topics/i18n/ 92 | 93 | LANGUAGE_CODE = 'en-us' 94 | 95 | TIME_ZONE = 'UTC' 96 | 97 | USE_I18N = True 98 | 99 | USE_L10N = True 100 | 101 | USE_TZ = True 102 | 103 | 104 | # Static files (CSS, JavaScript, Images) 105 | # https://docs.djangoproject.com/en/1.8/howto/static-files/ 106 | 107 | STATIC_URL = '/static/' 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django 1.8 Migration Bug Proof-of-Concept 2 | 3 | I've identified a bug in migrations in Django 1.8 w/ Postgres (that definitely 4 | wasn't present in v1.7). 5 | 6 | 7 | ## Bug Description 8 | 9 | If you have a field like: 10 | 11 | name = models.CharField(..., db_index=True) 12 | 13 | And you add a unique constraint without removing the `db_index=True`: 14 | 15 | name = models.CharField(..., db_index=True, unique=True) 16 | 17 | The auto-migration you get from that looks like: 18 | 19 | migrations.AlterField( 20 | model_name='person', 21 | name='name', 22 | field=models.CharField(db_index=True, unique=True, max_length=255, blank=True), 23 | ) 24 | 25 | When Django tries to run this migration you get: 26 | 27 | ``` 28 | Operations to perform: 29 | Synchronize unmigrated apps: staticfiles, messages 30 | Apply all migrations: admin, contenttypes, migration_poc_app, auth, sessions 31 | Synchronizing apps without migrations: 32 | Creating tables... 33 | Running deferred SQL... 34 | Installing custom SQL... 35 | Running migrations: 36 | Rendering model states... DONE 37 | Applying contenttypes.0001_initial... OK 38 | Applying auth.0001_initial... OK 39 | Applying admin.0001_initial... OK 40 | Applying contenttypes.0002_remove_content_type_name... OK 41 | Applying auth.0002_alter_permission_name_max_length... OK 42 | Applying auth.0003_alter_user_email_max_length... OK 43 | Applying auth.0004_alter_user_username_opts... OK 44 | Applying auth.0005_alter_user_last_login_null... OK 45 | Applying auth.0006_require_contenttypes_0002... OK 46 | Applying migration_poc_app.0001_initial... OK 47 | Applying migration_poc_app.0002_auto_20160114_2114...Traceback (most recent call last): 48 | File "manage.py", line 10, in 49 | execute_from_command_line(sys.argv) 50 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line 51 | utility.execute() 52 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute 53 | self.fetch_command(subcommand).run_from_argv(self.argv) 54 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv 55 | self.execute(*args, **cmd_options) 56 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute 57 | output = self.handle(*args, **options) 58 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle 59 | executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 60 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate 61 | self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) 62 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration 63 | state = migration.apply(state, schema_editor) 64 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply 65 | operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 66 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards 67 | schema_editor.alter_field(from_model, from_field, to_field) 68 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field 69 | old_db_params, new_db_params, strict) 70 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/schema.py", line 113, in _alter_field 71 | self.execute(like_index_statement) 72 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 111, in execute 73 | cursor.execute(sql, params) 74 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute 75 | return super(CursorDebugWrapper, self).execute(sql, params) 76 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 77 | return self.cursor.execute(sql, params) 78 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/utils.py", line 98, in __exit__ 79 | six.reraise(dj_exc_type, dj_exc_value, traceback) 80 | File "/Users/zack/src/migration_poc/venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 81 | return self.cursor.execute(sql, params) 82 | django.db.utils.ProgrammingError: relation "migration_poc_app_person_name_32437daa2f5b1fe9_like" already exists 83 | ``` 84 | 85 | **N.B.:** If you remove the `db_index=True` from the `AlterField(...)` call, 86 | the migration succeeds, even if the field definition in the model still has 87 | `db_index=True`. 88 | 89 | 90 | ## Repro Instructions 91 | 92 | This repo includes a minimal Django project that produces the error. 93 | 94 | 1. Create a virtualenv and install Django and psycopg2: 95 | 96 | ``` 97 | virtualenv venv 98 | . venv/bin/activate 99 | pip install -r requirements.txt 100 | ``` 101 | 102 | 2. Install Postgres, e.g. from [Postgres.app](http://postgresapp.com) 103 | 3. Edit `migration_poc/settings.py` to point to your local Postgres instance 104 | 4. Create a `migration_poc` database: 105 | 106 | ``` 107 | createdb migration_poc 108 | ``` 109 | 110 | 5. Run the migration: 111 | 112 | ``` 113 | python manage.py migrate 114 | ``` 115 | 116 | This should produce the error. 117 | 118 | You can make changes to the auto-migration and models.py, then just run `dropdb 119 | migration_poc; createdb migration_poc` to reset the DB and try migrating again. 120 | This is how I found out that removing the `db_index=True` param from 121 | `AlterField` side-steps the issue. 122 | --------------------------------------------------------------------------------