Opened 7 years ago
Closed 3 days ago
#30032 closed New feature (duplicate)
Allow expressions to be used for default
| Reported by: | Gavin Wahl | Owned by: | Johannes Maron |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Johannes Maron, Brian Helba, Ülgen Sarıkavak, Sage Abdullah | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (26)
comment:1 by , 7 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
comment:2 by , 7 years ago
This isn't a database level default, that ticket is to support defaults assigned by DDL. This is to support expressions as defaults.
comment:3 by , 7 years ago
| Resolution: | duplicate |
|---|---|
| Status: | closed → new |
| Summary: | Can't alter the default of a DateTimeField to be TransactionNow → Allow expressions to be used for default |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → New feature |
| Version: | 2.1 → master |
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.
comment:4 by , 7 years ago
Simon, you might be thinking of this PR which adds unofficial support for something like models.DateTimeField(default=Now) on PostgreSQL.
comment:5 by , 7 years ago
Thanks Tim, that's exactly what I had in mind. Should we close this ticket in favour of #29444 then?
comment:6 by , 7 years ago
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).
comment:8 by , 7 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
| Owner: | changed from to |
| Status: | new → assigned |
comment:10 by , 6 years ago
| Needs documentation: | unset |
|---|---|
| Needs tests: | unset |
comment:11 by , 6 years ago
| Patch needs improvement: | set |
|---|
comment:12 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:13 by , 6 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:14 by , 6 years ago
| Needs tests: | unset |
|---|---|
| Patch needs improvement: | unset |
comment:15 by , 6 years ago
| Patch needs improvement: | set |
|---|
comment:16 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:17 by , 6 years ago
| Patch needs improvement: | set |
|---|
comment:18 by , 6 years ago
| Patch needs improvement: | unset |
|---|
comment:19 by , 5 years ago
| Patch needs improvement: | set |
|---|
Marking as "needs improvement" per Simon's comments.
comment:20 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:21 by , 4 years ago
| Patch needs improvement: | set |
|---|
comment:22 by , 4 years ago
| Cc: | added |
|---|
comment:23 by , 3 years ago
Refreshed the PR with a rebase and conflict/style fixes, but it still needs work fixing failing tests.
comment:24 by , 23 months ago
| Cc: | added |
|---|
comment:25 by , 9 months ago
| Cc: | added |
|---|
comment:26 by , 3 days ago
| Resolution: | → duplicate |
|---|---|
| Status: | assigned → closed |
IMHO this has been resolved in #470
Datebase level defaults are not supported yet, see #470.