#34828: DateTimeField breaks when given datetime that would be invalid in UTC
-------------------------------------+-------------------------------------
               Reporter:  Denis      |          Owner:  nobody
  Cornehl                            |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  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          |
-------------------------------------+-------------------------------------
 We had a strange production error which I think is due to a Django bug,
 but please correct me if I'm wrong :)

 We have `USE_TZ` active, our own timezone set (Europe/Berlin), and a user
 entered `0001-01-01 00:00:00` into a datetimefield in a form.

 This value is now put into the postgres `timestamp with tz` field
 including its timezone (` 0001-01-01 00:00:28+00:53:28` for example).

 When I now try to read the model instance from the database, I'm getting a
 `ValueError: year -1 is out of range ` exception:

 {{{
   File "[...]/lib/python3.11/site-packages/django/db/models/manager.py",
 line 87, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "[...]/lib/python3.11/site-packages/django/db/models/query.py",
 line 633, in get
     num = len(clone)
           ^^^^^^^^^^
   File "[...]/lib/python3.11/site-packages/django/db/models/query.py",
 line 380, in __len__
     self._fetch_all()
   File "[...]/lib/python3.11/site-packages/django/db/models/query.py",
 line 1881, in _fetch_all
     self._result_cache = list(self._iterable_class(self))
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "[...]/lib/python3.11/site-packages/django/db/models/query.py",
 line 91, in __iter__
     results = compiler.execute_sql(
               ^^^^^^^^^^^^^^^^^^^^^
   File "[...]/lib/python3.11/site-
 packages/django/db/models/sql/compiler.py", line 1595, in execute_sql
     return list(result)
            ^^^^^^^^^^^^
   File "[...]/lib/python3.11/site-
 packages/django/db/models/sql/compiler.py", line 2093, in cursor_iter
     for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
   File "[...]/lib/python3.11/site-
 packages/django/db/models/sql/compiler.py", line 2093, in <lambda>
     for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "[...]/lib/python3.11/site-packages/django/db/utils.py", line 98,
 in inner
     return func(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^
 ValueError: year -1 is out of range

 }}}

 I'm aware that the postgres timestamp supports a wider range than the
 python datetime, but then I would have assumed that the field wouldn't
 even accept values it can't handle.
 In this specific case I see that we have a timezone aware datetime in
 python, store it into the database with timezone, and `psycopg2` fetches
 it as timezone aware datetime. So I'm not sure why it has to be valid in
 UTC too.


 This can be reproduced using a simple model:
 {{{#!python
 class TestModel(models.Model):
     d = models.DateTimeField()
 }}}

 And the following code:
 {{{#!python
 from brokendate.models import TestModel
 from datetime import datetime, date
 from pytz import timezone

 TestModel.objects.all().delete()
 ok = datetime(1,1,1, 0,0,0, tzinfo=timezone("Europe/Berlin"))
 t = TestModel()
 # assign datetime that would be invalid in UTC
 t.d = ok
 t.save()

 print("fetch object")
 # this raises the exception
 t = TestModel.objects.get()
 print(t.d)
 }}}

 I've created a small django project / app with a `break` management
 command that will reproduce this problem.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34828>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a837f8ae3-603b4320-40a9-4933-97cd-17b33f8af129-000000%40eu-central-1.amazonses.com.

Reply via email to