﻿
Log of Session Demonstrating Problem
------------------------------------

$ django-admin startproject problemdemo
$ cd problemdemo/
$ python manage.py startapp anapp
postgres=# create database problemdemo
postgres=# create user problemdemo;
postgres=> alter user problemdemo unencrypted password 'problemdemo';

** set up DATABASES and INSTALLED_APPS in settings.py **

$ python manage.py syncdb
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): no

** Initial model **
$ cat anapp/models.py
from django.db import models

# Create your models here.
class Demo(models.Model):
    """Simplest possible example of problem."""
    pkfield = models.IntegerField(primary_key=True)

$ python manage.py makemigrations anapp
Migrations for 'anapp':
  0001_initial.py:
    - Create model Demo
$ python manage.py migrate anapp
Operations to perform:
  Apply all migrations: anapp
Running migrations:
  Applying anapp.0001_initial... OK

** Create a record in table **
$ python manage.py shell
Python 3.3.2 (default, Jun  7 2013, 10:22:00)
In [1]: from anapp.models import Demo
In [2]: a = Demo(7)
In [3]: a.save()

** Remove primary_key from model **
$ cat anapp/models.py
from django.db import models

# Create your models here.
class Demo(models.Model):
    """Simplest possible example of problem."""
    pkfield = models.IntegerField()

** Attempt migration of existing table **
$ python manage.py makemigrations anapp
You are trying to add a non-nullable field 'id' to demo without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime module is available, so you can do e.g. datetime.date.today()
>>> 0
Migrations for 'anapp':
  0002_auto_20140710_1901.py:
    - Add field id to demo
    - Alter field pkfield on demo

$ python manage.py migrate anapp
Operations to perform:
  Apply all migrations: anapp
Running migrations:
  Applying anapp.0002_auto_20140710_1901...Traceback (most recent call last):
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: multiple default values specified for column "id" of table "anapp_demo"


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/executor.py", line 62, in migrate
    self.apply_migration(migration, fake=fake)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
    field,
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/schema.py", line 409, in add_field
    self.execute(sql, params)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/schema.py", line 98, in execute
    cursor.execute(sql, params)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/utils/six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "anapp_demo"

$ cat anapp/migrations/0002_auto_20140710_1901.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('anapp', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='demo',
            name='id',
            field=models.AutoField(serialize=False, auto_created=True, default=0, verbose_name='ID', primary_key=True),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='demo',
            name='pkfield',
            field=models.IntegerField(),
        ),
    ]

** Manually modify generated migration. Not operations order switched, "default=0, " removed.
$ cat anapp/migrations/0002_auto_20140710_1901.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('anapp', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='demo',
            name='pkfield',
            field=models.IntegerField(),
        ),
        migrations.AddField(
            model_name='demo',
            name='id',
            field=models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True),
            preserve_default=False,
        ),
    ]

$ python manage.py migrate anapp
Operations to perform:
  Apply all migrations: anapp
Running migrations:
  Applying anapp.0002_auto_20140710_1901... OK
