Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20587 closed Bug (fixed)

sqlite3 backend fails to properly handle null dates in certain cases.

Reported by: c@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords: sqlite3
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When converting from the model field to a django type db/sqlite3/base.py uses a method called convert_values to coerce certain types to match what is returned by other backends. However, in certain cases this fails. One such case is when the field is a DateTimeField and the value is null.

In that particular case, Django attempts to use parse_date() to coerce the value into a datetime value, which fails with a TypeError (since it is passed a null.)

I believe the proper behaviour here is to simply short-circuit the logic and return None if the value in the field is None, if it is not None, assume it fits the specified type and allow the method to proceed as normal.

So basically, this method definition:

    def convert_values(self, value, field):
        """SQLite returns floats when it should be returning decimals,
        and gets dates and datetimes wrong.
        For consistency with other backends, coerce when required.
        """
        if value is None:
            return None
        internal_type = field.get_internal_type()
        if internal_type == 'DecimalField':
            return util.typecast_decimal(field.format_number(value))
        elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
            return int(value)
        elif internal_type == 'DateField':
            return parse_date(value)
        elif internal_type == 'DateTimeField':
            return parse_datetime_with_timezone_support(value)
        elif internal_type == 'TimeField':
            return parse_time(value)

        # No field, or the field isn't known to be a decimal or integer
        return value

Change History (4)

comment:1 by Kamu, 11 years ago

Needs tests: set

Can you write a test where this would occur?

comment:2 by Anssi Kääriäinen, 11 years ago

Can't mark as accepted as there is no test case. All that is needed is a model definition + a little code to show a case where the error happens.

comment:3 by Gilberto Gonçalves <lursty@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 680b512fc1509ca05cf56fada30f6b833735362a:

Fixed #20587 -- Made convert_values handle None values

comment:4 by Andrew Godwin <andrew@…>, 11 years ago

In 864613c7e08ad6a5bc1737a5c21c711bd0385663:

Merge pull request #1295 from LuRsT/fix_sqlite_convert_values_20587

Fixed #20587 -- Made convert_values handle None values

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