1 |
|
---|
2 | Log of Session Demonstrating Problem
|
---|
3 | ------------------------------------
|
---|
4 |
|
---|
5 | $ django-admin startproject problemdemo
|
---|
6 | $ cd problemdemo/
|
---|
7 | $ python manage.py startapp anapp
|
---|
8 | postgres=# create database problemdemo
|
---|
9 | postgres=# create user problemdemo;
|
---|
10 | postgres=> alter user problemdemo unencrypted password 'problemdemo';
|
---|
11 |
|
---|
12 | ** set up DATABASES and INSTALLED_APPS in settings.py **
|
---|
13 |
|
---|
14 | $ python manage.py syncdb
|
---|
15 | Operations to perform:
|
---|
16 | Apply all migrations: admin, auth, contenttypes, sessions
|
---|
17 | Running migrations:
|
---|
18 | Applying contenttypes.0001_initial... OK
|
---|
19 | Applying auth.0001_initial... OK
|
---|
20 | Applying admin.0001_initial... OK
|
---|
21 | Applying sessions.0001_initial... OK
|
---|
22 |
|
---|
23 | You have installed Django's auth system, and don't have any superusers defined.
|
---|
24 | Would you like to create one now? (yes/no): no
|
---|
25 |
|
---|
26 | ** Initial model **
|
---|
27 | $ cat anapp/models.py
|
---|
28 | from django.db import models
|
---|
29 |
|
---|
30 | # Create your models here.
|
---|
31 | class Demo(models.Model):
|
---|
32 | """Simplest possible example of problem."""
|
---|
33 | pkfield = models.IntegerField(primary_key=True)
|
---|
34 |
|
---|
35 | $ python manage.py makemigrations anapp
|
---|
36 | Migrations for 'anapp':
|
---|
37 | 0001_initial.py:
|
---|
38 | - Create model Demo
|
---|
39 | $ python manage.py migrate anapp
|
---|
40 | Operations to perform:
|
---|
41 | Apply all migrations: anapp
|
---|
42 | Running migrations:
|
---|
43 | Applying anapp.0001_initial... OK
|
---|
44 |
|
---|
45 | ** Create a record in table **
|
---|
46 | $ python manage.py shell
|
---|
47 | Python 3.3.2 (default, Jun 7 2013, 10:22:00)
|
---|
48 | In [1]: from anapp.models import Demo
|
---|
49 | In [2]: a = Demo(7)
|
---|
50 | In [3]: a.save()
|
---|
51 |
|
---|
52 | ** Remove primary_key from model **
|
---|
53 | $ cat anapp/models.py
|
---|
54 | from django.db import models
|
---|
55 |
|
---|
56 | # Create your models here.
|
---|
57 | class Demo(models.Model):
|
---|
58 | """Simplest possible example of problem."""
|
---|
59 | pkfield = models.IntegerField()
|
---|
60 |
|
---|
61 | ** Attempt migration of existing table **
|
---|
62 | $ python manage.py makemigrations anapp
|
---|
63 | You are trying to add a non-nullable field 'id' to demo without a default;
|
---|
64 | we can't do that (the database needs something to populate existing rows).
|
---|
65 | Please select a fix:
|
---|
66 | 1) Provide a one-off default now (will be set on all existing rows)
|
---|
67 | 2) Quit, and let me add a default in models.py
|
---|
68 | Select an option: 1
|
---|
69 | Please enter the default value now, as valid Python
|
---|
70 | The datetime module is available, so you can do e.g. datetime.date.today()
|
---|
71 | >>> 0
|
---|
72 | Migrations for 'anapp':
|
---|
73 | 0002_auto_20140710_1901.py:
|
---|
74 | - Add field id to demo
|
---|
75 | - Alter field pkfield on demo
|
---|
76 |
|
---|
77 | $ python manage.py migrate anapp
|
---|
78 | Operations to perform:
|
---|
79 | Apply all migrations: anapp
|
---|
80 | Running migrations:
|
---|
81 | Applying anapp.0002_auto_20140710_1901...Traceback (most recent call last):
|
---|
82 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
|
---|
83 | return self.cursor.execute(sql, params)
|
---|
84 | psycopg2.ProgrammingError: multiple default values specified for column "id" of table "anapp_demo"
|
---|
85 |
|
---|
86 |
|
---|
87 | The above exception was the direct cause of the following exception:
|
---|
88 |
|
---|
89 | Traceback (most recent call last):
|
---|
90 | File "manage.py", line 10, in <module>
|
---|
91 | execute_from_command_line(sys.argv)
|
---|
92 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
|
---|
93 | utility.execute()
|
---|
94 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/__init__.py", line 377, in execute
|
---|
95 | self.fetch_command(subcommand).run_from_argv(self.argv)
|
---|
96 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/base.py", line 288, in run_from_argv
|
---|
97 | self.execute(*args, **options.__dict__)
|
---|
98 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/base.py", line 337, in execute
|
---|
99 | output = self.handle(*args, **options)
|
---|
100 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/core/management/commands/migrate.py", line 160, in handle
|
---|
101 | executor.migrate(targets, plan, fake=options.get("fake", False))
|
---|
102 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/executor.py", line 62, in migrate
|
---|
103 | self.apply_migration(migration, fake=fake)
|
---|
104 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
|
---|
105 | migration.apply(project_state, schema_editor)
|
---|
106 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/migration.py", line 107, in apply
|
---|
107 | operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
|
---|
108 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
|
---|
109 | field,
|
---|
110 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/schema.py", line 409, in add_field
|
---|
111 | self.execute(sql, params)
|
---|
112 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/schema.py", line 98, in execute
|
---|
113 | cursor.execute(sql, params)
|
---|
114 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 81, in execute
|
---|
115 | return super(CursorDebugWrapper, self).execute(sql, params)
|
---|
116 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
|
---|
117 | return self.cursor.execute(sql, params)
|
---|
118 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/utils.py", line 94, in __exit__
|
---|
119 | six.reraise(dj_exc_type, dj_exc_value, traceback)
|
---|
120 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/utils/six.py", line 549, in reraise
|
---|
121 | raise value.with_traceback(tb)
|
---|
122 | File "/mnt/extra/python-environments/toms_too/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
|
---|
123 | return self.cursor.execute(sql, params)
|
---|
124 | django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "anapp_demo"
|
---|
125 |
|
---|
126 | $ cat anapp/migrations/0002_auto_20140710_1901.py
|
---|
127 | # -*- coding: utf-8 -*-
|
---|
128 | from __future__ import unicode_literals
|
---|
129 |
|
---|
130 | from django.db import models, migrations
|
---|
131 |
|
---|
132 |
|
---|
133 | class Migration(migrations.Migration):
|
---|
134 |
|
---|
135 | dependencies = [
|
---|
136 | ('anapp', '0001_initial'),
|
---|
137 | ]
|
---|
138 |
|
---|
139 | operations = [
|
---|
140 | migrations.AddField(
|
---|
141 | model_name='demo',
|
---|
142 | name='id',
|
---|
143 | field=models.AutoField(serialize=False, auto_created=True, default=0, verbose_name='ID', primary_key=True),
|
---|
144 | preserve_default=False,
|
---|
145 | ),
|
---|
146 | migrations.AlterField(
|
---|
147 | model_name='demo',
|
---|
148 | name='pkfield',
|
---|
149 | field=models.IntegerField(),
|
---|
150 | ),
|
---|
151 | ]
|
---|
152 |
|
---|
153 | ** Manually modify generated migration. Not operations order switched, "default=0, " removed.
|
---|
154 | $ cat anapp/migrations/0002_auto_20140710_1901.py
|
---|
155 | # -*- coding: utf-8 -*-
|
---|
156 | from __future__ import unicode_literals
|
---|
157 |
|
---|
158 | from django.db import models, migrations
|
---|
159 |
|
---|
160 |
|
---|
161 | class Migration(migrations.Migration):
|
---|
162 |
|
---|
163 | dependencies = [
|
---|
164 | ('anapp', '0001_initial'),
|
---|
165 | ]
|
---|
166 |
|
---|
167 | operations = [
|
---|
168 | migrations.AlterField(
|
---|
169 | model_name='demo',
|
---|
170 | name='pkfield',
|
---|
171 | field=models.IntegerField(),
|
---|
172 | ),
|
---|
173 | migrations.AddField(
|
---|
174 | model_name='demo',
|
---|
175 | name='id',
|
---|
176 | field=models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True),
|
---|
177 | preserve_default=False,
|
---|
178 | ),
|
---|
179 | ]
|
---|
180 |
|
---|
181 | $ python manage.py migrate anapp
|
---|
182 | Operations to perform:
|
---|
183 | Apply all migrations: anapp
|
---|
184 | Running migrations:
|
---|
185 | Applying anapp.0002_auto_20140710_1901... OK
|
---|