Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#26183 closed Bug (wontfix)

PostgreSQL backend: TimeField is created without time zone

Reported by: Stefan Heinemann Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: postgres, timezone, time field
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation states that "The PostgreSQL backend stores datetimes as timestamp with time zone."
Sadly, it does not do that with the TimeField, which can easily be seen in db/backend/postgres_psycopg2/base.py line 97. In my opinion, it would be nice to have the same behaviour for both the DateTimeField and the TimeField.

It's easy to change this line to

        'TimeField': 'time with time zone'

which does work. But there is a problem:
I wanted to do a pull request so I changed the line. Now I had problems with my migrations file, since I have one that alters a field from TimeField (which was now "with time zone") to a interval. Postgres does not like that and throws

	django.db.utils.ProgrammingError: column "duration" cannot be cast automatically to type interval
	HINT:  You might need to specify "USING duration::interval".

So I did that and executed, by hand:

	ALTER TABLE "resource_check_testtyperesourcetype" ALTER COLUMN "duration" TYPE interval USING duration::interval;

Now it says: ERROR: cannot cast type time with time zone to interval at character 103

So now I'm not sure if it is even a good idea to change the default behaviour of TimeField to "with time zone"....

Change History (2)

comment:1 by Tim Graham, 9 years ago

Resolution: wontfix
Status: newclosed
Summary: Postgres backend: Time Field is created without time zone, cascading problems when changing thisPostgreSQL backend: TimeField is created without time zone

This is by design as documented:

Python’s datetime.time objects also feature a tzinfo attribute, and PostgreSQL has a matching time with time zone type. However, as PostgreSQL’s docs put it, this type “exhibits properties which lead to questionable usefulness”.

Django only supports naive time objects and will raise an exception if you attempt to save an aware time object, as a timezone for a time with no associated date does not make sense.

comment:2 by Stefan Heinemann, 9 years ago

Right, thank you for clarification.

Note: See TracTickets for help on using tickets.
Back to Top