tortoise / tortoise/tortoise-orm

DatetimeField Field identification error

Open
#1,808 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

oracle
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
I have a project that is a table created by django with a time field called models.DateTimeField that records the time in the database as follows: 2022-07-29 11:00:24.794
I created a new fastapi project, using tortoise-orm to connect to Oracle database
There are two problems:

  1. When I define the time field in the model, if I define the field like this:
    EDIT_DATE = fields.DatetimeField(description=' Modify date ')
    The following error is reported:
    Traceback (most recent call last):
    File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 693, in lifespan
    async with self.lifespan_context(app) as maybe_state:
    File "/usr/lib64/python3.11/contextlib.py", line 210, in aenter
    return await anext(self.gen)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/home/whatsup_alarm/core/event.py", line 29, in lifespan
    await get_whatsup_servers_information()
    File "/home/whatsup_alarm/core/event.py", line 41, in get_whatsup_servers_information
    objs = await NetDeviceSysMonitor.filter(**filter_par)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/queryset.py", line 1151, in _execute
    instance_list = await self._db.executor_class(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/backends/base/executor.py", line 139, in execute_select
    instance: "Model" = self.model._init_from_db(**dict(row_items[:current_idx]))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/models.py", line 754, in _init_from_db
    setattr(self, model_field, field.to_python_value(kwargs[key]))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/tortoise/fields/data.py", line 354, in to_python_value
    value = parse_datetime(value)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/lib/python3.11/site-packages/iso8601/iso8601.py", line 124, in parse_date
    raise ParseError(e)
    iso8601.iso8601.ParseError: expected string or bytes-like object, got 'datetime.date'

If I define this field like this:
EDIT_DATE = fields.DateField(description=' Modify date ')
I can only get the date information, not the time information

If I define this field like this:
EDIT_DATE = fields.TimeField(description=' Modify date ')
The following error is reported:
ERROR: Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 693, in lifespan
async with self.lifespan_context(app) as maybe_state:
File "/usr/lib64/python3.11/contextlib.py", line 210, in aenter
return await anext(self.gen)
^^^^^^^^^^^^^^^^^^^^^
File "/home/whatsup_alarm/core/event.py", line 29, in lifespan
await get_whatsup_servers_information()
File "/home/whatsup_alarm/core/event.py", line 41, in get_whatsup_servers_information
objs = await NetDeviceSysMonitor.filter(**filter_par)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/queryset.py", line 1151, in _execute
instance_list = await self._db.executor_class(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/backends/base/executor.py", line 139, in execute_select
instance: "Model" = self.model._init_from_db(**dict(row_items[:current_idx]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/models.py", line 754, in _init_from_db
setattr(self, model_field, field.to_python_value(kwargs[key]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/fields/data.py", line 444, in to_python_value
if timezone.is_naive(value):
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/tortoise/timezone.py", line 84, in is_naive
return value.utcoffset() is None
^^^^^^^^^^^^^^^
AttributeError: 'datetime.date' object has no attribute 'utcoffset'

  1. The field names in the database are all uppercase. When I use lowercase field names in the model definition, I get an error, indicating that the field name is illegal

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Oracle reproduction and trace the failures through tortoise/fields/data.py, tortoise/timezone.py, tortoise/models.py, and tortoise/backends/base/executor.py. Separate the datetime conversion failure from the uppercase field-name problem, then add regression coverage showing the expected behavior for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.