Lee Hinde
Lee Hinde

Reputation: 1093

Django migration error, trying to clean up date field with bad default

I have a model:

class Season(models.Model):
    """ Corresponds to your brochure publishing schedule, e.g. Fall/Winter 2012, Sprint 2014, etc. """
    name = models.CharField(max_length=45, db_index=True, unique=True)
    start_date = models.DateField(db_index=True, help_text="The date enrollment can begin this Season.")

    <snip>

The model pre-dates migrations and I didn't use South.

The start_date field lived for many years with this definition

start_date = models.DateField(db_index=True, default=False,
                                  help_text="The date of the first Session of this Season.")

I actually went in to edit the help text and noticed the default=False and thought, that doesnt make sense for a date field, it must have been a remnant from when I was dumb.

So I took that out.

Now my migration:

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),
        ),
    ]

Fails with:

 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 "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/verylongpathtovenv/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 "/verylongpathtovenv/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 "/verylongpathtovenv/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/verylongpathtovenv/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 "/verylongpathtovenv/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 "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 566, in _alter_field
    old_default = self.effective_default(old_field)
  File "/verylongpathtovenv/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 "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
    prepared=False)
  File "/verylongpathtovenv/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 "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1317, in get_prep_value
    return self.to_python(value)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1274, in to_python
    parsed = parse_date(value)
  File "/verylongpathtovenv/lib/python2.7/site-packages/django/utils/dateparse.py", line 60, in parse_date
    match = date_re.match(value)
TypeError: expected string or buffer

Researching TypeError: expected string or buffer comes up with a discussion about not doing what I'm trying to undo, i.e, don't set the default for a date field to something not date-like.

Not clear how to proceed with this. Any tips would be appreciated.

Upvotes: 0

Views: 577

Answers (1)

2ps
2ps

Reputation: 15926

  1. Determine what the default is in the database

    cd /path/to/project
    python manage.py dbshell
    mysql> show create table district_season\G 
    # or for postgres
    # pg_dump -t 'schema.district_season' --schema-only database-name
    
  2. If there is no problem with the type, run the migration with --fake

    ./manage.py migrate district --fake
    

    which will mark the migration as having been run without doing anything to the database.

Upvotes: 1

Related Questions