Django Timefield(default=<TimeDelta>) gives unhelpful diagnostics

Bug #1875596 reported by Simon Lambourn
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-django (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

(Be warned, I am a novice and this may be a novice mistake):

I mistakenly added a TimeField instead of a DurationField to my model. I was forced to give it a default value and chose TimeDelta(minutes=30). The makemigration worked, but the migrate command crashed with very unhelpful diagnostics, not even indicating which field caused the problem.

I'm hoping that this lack of diagnostics could be improved, to at least point to the database column that caused the problem?

Thanks in advance for all the good stuff you do :-)
Simon Lambourn.

Output from manage.py migrate:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, kitten, sessions
Running migrations:
  Applying kitten.0010_auto_20200428_0957...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3/dist-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/lib/python3/dist-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/lib/python3/dist-packages/django/db/migrations/operations/fields.py", line 88, in database_forwards
    field,
  File "/usr/lib/python3/dist-packages/django/db/backends/sqlite3/schema.py", line 240, in add_field
    self._remake_table(model, create_field=field)
  File "/usr/lib/python3/dist-packages/django/db/backends/sqlite3/schema.py", line 115, in _remake_table
    self.effective_default(create_field)
  File "/usr/lib/python3/dist-packages/django/db/backends/base/schema.py", line 245, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/usr/lib/python3/dist-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "/usr/lib/python3/dist-packages/django/db/models/fields/__init__.py", line 2281, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/lib/python3/dist-packages/django/db/models/fields/__init__.py", line 2276, in get_prep_value
    return self.to_python(value)
  File "/usr/lib/python3/dist-packages/django/db/models/fields/__init__.py", line 2250, in to_python
    parsed = parse_time(value)
  File "/usr/lib/python3/dist-packages/django/utils/dateparse.py", line 76, in parse_time
    match = time_re.match(value)
TypeError: expected string or bytes-like object

ProblemType: Bug
DistroRelease: Ubuntu 19.10
Package: python3-django 1:1.11.22-1ubuntu1.3
ProcVersionSignature: Ubuntu 5.3.0-46.38-generic 5.3.18
Uname: Linux 5.3.0-46-generic x86_64
ApportVersion: 2.20.11-0ubuntu8.8
Architecture: amd64
CurrentDesktop: ubuntu:GNOME
Date: Tue Apr 28 09:59:25 2020
PackageArchitecture: all
SourcePackage: python-django
UpgradeStatus: Upgraded to eoan on 2019-10-23 (187 days ago)

Revision history for this message
Simon Lambourn (simon-lambourn-o) wrote :
Revision history for this message
Rafael David Tinoco (rafaeldtinoco) wrote :

Local Configuration Issue

Thank you for taking the time to file a bug report.

Since it seems likely to me that this is a local configuration problem,
rather than a bug in Ubuntu, I am marking this bug as 'Invalid'.

However, if you believe that this is really a bug in Ubuntu, then we would
be grateful if you would provide a more complete description of the problem
with steps to reproduce, explain why you believe this is a bug in Ubuntu
rather than a problem specific to your system, and then change the bug
status back to "New".

For local configuration issues, you can find assistance here:
http://www.ubuntu.com/support/community

Changed in python-django (Ubuntu):
status: New → Invalid
Revision history for this message
Simon Lambourn (simon-lambourn-o) wrote :

Dear Rafael,
Thank you for responding to my bug report. I'm sorry I didn't provide the full information you ask for, and based on your feedback I'm not sure whether this is a bug or not. It is certainly a user error, but the bug I'm hoping to raise is with the unhelpful diagnostics, rather than my own mistake.

I've provided more info below. I would be grateful if you could take a look and decide whether this qualifies as a bug - then based on your advice I will be happy to re-open the bug or not.

Steps to reproduce the bug:
- Define a model class as follows
    class Test(models.Model):
        testfield = models.TimeField(default=datetime.timedelta(0))
        # I acknowledge this is a user error but it is accepted by Python, and by makemigration.

= run makemigration (which works OK) and migrate (which fails, giving the output described above).

Desired output:
An error message from the migrate command, stating that a TypeError occurred setting a value to this field, with either model name/field name, or a source line number.

Thanks again for your advice.

Revision history for this message
Christian Ehrhardt  (paelzer) wrote :

Hi Simon,
as you already came to a conclusion with Rafael this isn't much of a bug. Even less so in the Ubuntu packaging or integration of it. But I agree that it would be a nice feature request to the upstream project to enhance the error case with some way more usable information to help.

I have never gone deep on django, but if in [1] really is nothing that makes this better I'd recommend to file a feature request at [2].
If you happen to do so we'd be glad about dropping a link to it here so we can better track its integration into Ubuntu down the road.

[1]: https://docs.djangoproject.com/en/3.0/howto/error-reporting/
[2]: https://code.djangoproject.com/wiki

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.