[Django] #30032: Can't alter the default of a DateTimeField to be TransactionNow

48 views
Skip to first unread message

Django

unread,
Dec 11, 2018, 1:05:50 PM12/11/18
to django-...@googlegroups.com
#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.

Django

unread,
Dec 11, 2018, 1:48:11 PM12/11/18
to django-...@googlegroups.com
#30032: Can't alter the default of a DateTimeField to be TransactionNow
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: new => closed
* resolution: => duplicate


Comment:

Datebase level defaults are not supported yet, see #470.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:1>

Django

unread,
Dec 11, 2018, 3:25:58 PM12/11/18
to django-...@googlegroups.com
#30032: Can't alter the default of a DateTimeField to be TransactionNow
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Gavin Wahl):

This isn't a database level default, that ticket is to support defaults
assigned by DDL. This is to support expressions as defaults.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:2>

Django

unread,
Dec 11, 2018, 4:49:02 PM12/11/18
to django-...@googlegroups.com
#30032: Allow expressions to be used for default

-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: closed => new
* type: Bug => New feature
* version: 2.1 => master
* resolution: duplicate =>
* stage: Unreviewed => Accepted


Comment:

Sorry about the misinterpretation, #27222 seems related and I'm pretty
sure there's another ticket related to allowing `Now()` to be used for
`default` but I can't find it.

Tentatively accepting as a new feature request but as I said I'm pretty
sure there is already another ticket tracking this.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:3>

Django

unread,
Dec 20, 2018, 10:45:08 PM12/20/18
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Simon, you might be thinking of this
[https://github.com/django/django/pull/9983 PR] which adds unofficial
support for something like `models.DateTimeField(default=Now)` on
PostgreSQL.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:4>

Django

unread,
Dec 21, 2018, 10:27:46 AM12/21/18
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Thanks Tim, that's exactly what I had in mind. Should we close this ticket
in favour of #29444 then?

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:5>

Django

unread,
Dec 21, 2018, 1:01:47 PM12/21/18
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

I think we should keep this open as there will be more work to do even if
that ticket is closed (adding support for other databases and making the
support official with documentation).

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:6>

Django

unread,
Dec 21, 2018, 1:22:20 PM12/21/18
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Makes sense, thanks for chiming in.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:7>

Django

unread,
Nov 21, 2019, 11:18:10 AM11/21/19
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* cc: Johannes Hoppe (added)
* owner: nobody => Johannes Hoppe
* has_patch: 0 => 1
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:8>

Django

unread,
Dec 11, 2019, 7:21:16 AM12/11/19
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_docs: 0 => 1
* needs_tests: 0 => 1


Comment:

Simon left comments.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:9>

Django

unread,
Jan 31, 2020, 5:09:37 AM1/31/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_docs: 1 => 0
* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:10>

Django

unread,
Feb 1, 2020, 1:42:13 PM2/1/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:11>

Django

unread,
Feb 1, 2020, 5:56:03 PM2/1/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:12>

Django

unread,
Jul 21, 2020, 6:15:22 AM7/21/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:13>

Django

unread,
Sep 1, 2020, 1:31:52 PM9/1/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 1 => 0

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:14>

Django

unread,
Sep 3, 2020, 3:55:44 AM9/3/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:15>

Django

unread,
Sep 4, 2020, 5:25:21 AM9/4/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:16>

Django

unread,
Oct 23, 2020, 8:04:37 PM10/23/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Hoppe
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by InvalidInterrupt):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:17>

Django

unread,
Nov 14, 2020, 12:56:02 PM11/14/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Maron):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:18>

Django

unread,
Dec 18, 2020, 4:29:08 AM12/18/20
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


Comment:

Marking as "needs improvement" per Simon's comments.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:19>

Django

unread,
Feb 21, 2021, 5:54:15 AM2/21/21
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Johannes Maron):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:20>

Django

unread,
Mar 5, 2022, 6:22:14 AM3/5/22
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron
Type: New feature | Status: assigned
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:21>

Django

unread,
Jun 23, 2022, 4:30:34 AM6/23/22
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Brian Helba):

* cc: Brian Helba (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:22>

Django

unread,
Oct 30, 2023, 3:22:21 PM10/30/23
to django-...@googlegroups.com
#30032: Allow expressions to be used for default
-------------------------------------+-------------------------------------
Reporter: Gavin Wahl | Owner: Johannes
| Maron
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Natalia Bidart):

Refreshed [https://github.com/django/django/pull/11783/ the PR] with a
rebase and conflict/style fixes, but it still needs work fixing failing
tests.

--
Ticket URL: <https://code.djangoproject.com/ticket/30032#comment:23>

Reply all
Reply to author
Forward
0 new messages