Code

Opened 10 months ago

Closed 10 months ago

Last modified 10 months 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

Attachments (0)

Change History (4)

comment:1 Changed 10 months ago by Kamu

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

Can you write a test where this would occur?

comment:2 Changed 10 months ago by akaariai

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 Changed 10 months ago by Gilberto Gonçalves <lursty@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 680b512fc1509ca05cf56fada30f6b833735362a:

Fixed #20587 -- Made convert_values handle None values

comment:4 Changed 10 months ago by Andrew Godwin <andrew@…>

In 864613c7e08ad6a5bc1737a5c21c711bd0385663:

Merge pull request #1295 from LuRsT/fix_sqlite_convert_values_20587

Fixed #20587 -- Made convert_values handle None values

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.