Problems with migrations cleaning up a date field

22 views
Skip to first unread message

Lee Hinde

unread,
Jun 10, 2017, 1:09:24 AM6/10/17
to django...@googlegroups.com
I'm having problems with a migration after cleaning up an oddity with a date field and I'm not sure how to fix it.  Any advice appreciated.

I made this change to a date field:

-    start_date = models.DateField(db_index=True, default=False,
-                                  help_text="The date enrollment can begin this Season.")
+    start_date = models.DateField(db_index=True, help_text="The date enrollment can begin this Season.")

I'm not sure how a default of False ever made it, but I ran across it and fixed it.

This is the migration that was created:

class Migration(migrations.Migration):

    dependencies = [
        ('district', '0012_auto_20160622_1741'),
    ]

    operations = [
        migrations.AlterField(
            model_name='season',
            name='start_date',
            field=models.DateField(help_text=b'The date enrollment can begin this Season.', db_index=True),
        ),
    ]

This is the error I get trying to run the migration:

  Applying district.0013_auto_20170204_1811...Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 566, in _alter_field
    old_default = self.effective_default(old_field)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
    prepared=False)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1322, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
    return self.to_python(value)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1274, in to_python
    parsed = parse_date(value)
  File "/Users/leehinde/Documents/Clients/rnrvirtual/venv-python2.7-django/lib/python2.7/site-packages/django/utils/dateparse.py", line 60, in parse_date
    match = date_re.match(value)
TypeError: expected string or buffer

Lee Hinde

unread,
Jun 10, 2017, 1:11:57 AM6/10/17
to django...@googlegroups.com
I should add, this project is in 1.8.x. I'm working around the problem by skipping the migration.

James Schneider

unread,
Jun 10, 2017, 3:57:07 AM6/10/17
to django...@googlegroups.com


On Jun 9, 2017 6:09 PM, "Lee Hinde" <leeh...@gmail.com> wrote:
I'm having problems with a migration after cleaning up an oddity with a date field and I'm not sure how to fix it.  Any advice appreciated.

I made this change to a date field:

-    start_date = models.DateField(db_index=True, default=False,
-                                  help_text="The date enrollment can begin this Season.")
+    start_date = models.DateField(db_index=True, help_text="The date enrollment can begin this Season.")

I'm not sure how a default of False ever made it, but I ran across it and fixed it.

Have you verified that every row has a value for start_date? The error sounds like either your start_dates are missing/have null values, or you have a bad value somewhere.

Quite honestly this sounds like a bug in the migration system if that's the case. I'm pretty sure it should ask what value to use as a filler value during the migration, although I haven't had a change like that in a while.

-James

Melvyn Sopacua

unread,
Jun 10, 2017, 12:04:49 PM6/10/17
to django...@googlegroups.com

On Friday 09 June 2017 20:56:46 James Schneider wrote:

 

> Quite honestly this sounds like a bug in the migration system if

> that's the case. I'm pretty sure it should ask what value to use as a

> filler value during the migration, although I haven't had a change

> like that in a while.

 

When you *remove* a default, there is no need to migrate existing rows, as they have a default.

--

Melvyn Sopacua

Melvyn Sopacua

unread,
Jun 10, 2017, 12:16:45 PM6/10/17
to django...@googlegroups.com

On Friday 09 June 2017 18:08:39 Lee Hinde wrote:

> I'm having problems with a migration after cleaning up an oddity with

> a date field and I'm not sure how to fix it. Any advice appreciated.

>

> I made this change to a date field:

>

> - start_date = models.DateField(db_index=True, default=False,

> - help_text="The date enrollment can

> begin this Season.")

 

Apparently, this goes through, even though it's not a valid default for a date field (tested with 1.11.2). So that is the bug in the migrations system.

 

Is it possible for you to rollback to before this migration or has this hit production already?

--

Melvyn Sopacua

Lee Hinde

unread,
Jun 12, 2017, 6:09:24 PM6/12/17
to django...@googlegroups.com
I should have mentioned that I checked for empty date fields first and they were all populated.

My work around was to skip the migration. Just redid the dependency on the next migration and all was well.

Thanks.
Reply all
Reply to author
Forward
0 new messages