#30032: Can't alter the default of a DateTimeField to be TransactionNow
-------------------------------------+-------------------------------------
Reporter: Gavin | Owner: nobody
Wahl |
Type: Bug | Status: new
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have this field:
{{{
foo = models.DateTimeField(default=timezone.now)
}}}
I would like to modify it to use TransactionNow instead:
{{{
foo = models.DateTimeField(default=TransactionNow())
}}}
This migration operation is generated:
{{{
migrations.AlterField(
model_name='somemodel',
name='foo',
field=models.DateTimeField(default=django.contrib.postgres.functions.TransactionNow()),
),
}}}
When run, this error is thrown:
{{{
File "./manage.py", line 29, in <module>
execute_from_command_line(sys.argv)
File "django/core/management/__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
fake_initial=fake_initial)
File "django/db/migrations/executor.py", line 147, in
_migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File "django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "django/db/migrations/operations/fields.py", line 216, in
database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "django/db/backends/base/schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "django/db/backends/postgresql/schema.py", line 122, in
_alter_field
new_db_params, strict,
File "django/db/backends/base/schema.py", line 626, in _alter_field
old_default = self.effective_default(old_field)
File "django/db/backends/base/schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "django/db/models/fields/__init__.py", line 790, in
get_db_prep_save
return self.get_db_prep_value(value, connection=connection,
prepared=False)
File "django/db/models/fields/__init__.py", line 1429, in
get_db_prep_value
value = self.get_prep_value(value)
File "django/db/models/fields/__init__.py", line 1408, in get_prep_value
value = super().get_prep_value(value)
File "django/db/models/fields/__init__.py", line 1268, in get_prep_value
return self.to_python(value)
File "django/db/models/fields/__init__.py", line 1369, in to_python
parsed = parse_datetime(value)
File "django/utils/dateparse.py", line 106, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
}}}
If I create the field foo from scratch, with an AddField operation instead
of AlterField, it works as expected.
I should be able to modify a field to use TransactionNow() as a default.
--
Ticket URL: <https://code.djangoproject.com/ticket/30032>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.8c03ee2bb471c66c8d2b1bb7ad8aa34f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.