[PATCH 3/3] migrations: Add the default value of Patch.state

Damien Lespiau damien.lespiau at intel.com
Wed Aug 19 03:30:20 AEST 2015

Now that we have migration 0002 populating the db with the intial list
of states, we can add the default Patch.state value (which depends on
being able to query the state with ordering=0).

Of course, it's not that simple and I was hitting an exception

  Applying patchwork.0003_patch_state_default_value...Traceback (most recent call last):
  File "django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "django/db/backends/base/schema.py", line 567, in _alter_field
    new_default = self.effective_default(new_field)
  File "django/db/backends/base/schema.py", line 211, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "django/db/models/fields/related.py", line 1956, in get_db_prep_save
    return self.related_field.get_db_prep_save(value, connection=connection)
  File "django/db/models/fields/__init__.py", line 710, in get_db_prep_save
  File "django/db/models/fields/__init__.py", line 977, in get_db_prep_value
    value = self.get_prep_value(value)
  File "django/db/models/fields/__init__.py", line 985, in get_prep_value
    return int(value)
  TypeError: int() argument must be a string or a number, not 'State'

Django was trying to make an int out of the State instance returned by
get_default_initial_patch_state(). Turns out I wasn't the first one to
hit that:

  "Migration crash on ForeignKey with a callable default that returns a
   model instance"

  Source: https://code.djangoproject.com/ticket/25129

And the solution decided by the django developers was to restrict what
callable defaults can return with ForeignKeys:

  "For fields like ForeignKey that map to model instances, defaults
   should be the value of the field they reference (pk unless to_field is
   set) instead of model instances."

  Source: https://docs.djangoproject.com/en/dev/ref/models/fields/#default

Signed-off-by: Damien Lespiau <damien.lespiau at intel.com>
 .../migrations/0003_patch_state_default_value.py     | 20 ++++++++++++++++++++
 patchwork/models.py                                  |  2 +-
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 patchwork/migrations/0003_patch_state_default_value.py

diff --git a/patchwork/migrations/0003_patch_state_default_value.py b/patchwork/migrations/0003_patch_state_default_value.py
new file mode 100644
index 0000000..9941c0b
--- /dev/null
+++ b/patchwork/migrations/0003_patch_state_default_value.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+from django.db import models, migrations
+import patchwork.models
+class Migration(migrations.Migration):
+    dependencies = [
+        ('patchwork', '0002_load_initial_data'),
+    ]
+    operations = [
+        migrations.AlterField(
+            model_name='patch',
+            name='state',
+            field=models.ForeignKey(default=patchwork.models.get_default_initial_patch_state, to='patchwork.State'),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index c2b8a9c..92fe6ee 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -203,7 +203,7 @@ class PatchTag(models.Model):
         unique_together = [('patch', 'tag')]
 def get_default_initial_patch_state():
-    return State.objects.get(ordering=0)
+    return State.objects.get(ordering=0).pk
 class PatchQuerySet(models.query.QuerySet):

More information about the Patchwork mailing list