igorbenav/fastcrud

can't subtract offset-naive and offset-aware datetimes

Closed this issue · 2 comments

When deleting an object, the following occurs: can't subtract offset-naive and offset-aware datetimes.
Below I wrote the model and parameters that are passed in the SQL query.

Here is model with fields for soft delete

class UserModel(Base):
...
    created_at = Column(DateTime(), default=datetime.now())
    updated_at = Column(
        DateTime(),
        default=datetime.now(),
        onupdate=datetime.now(),
    )
    deleted_at = Column(DateTime())
    is_deleted = Column(Boolean, default=False)
user_crud = FastCRUD(UserModel)
user_router = crud_router(
    session=get_session,
    model=UserModel,
    crud=user_crud,
    create_schema=UserSchema.UserCreate,
    update_schema=UserSchema.UserUpdate,
    path="/users",
    tags=["Users"]
)

parameters:

[SQL: UPDATE users SET updated_at=$1::TIMESTAMP WITHOUT TIME ZONE, deleted_at=$2::TIMESTAMP WITHOUT TIME ZONE, is_deleted=$3::BOOLEAN WHERE parking.id = $4::INTEGER]
[parameters: (datetime.datetime(2024, 2, 22, 2, 54, 42, 666156), datetime.datetime(2024, 2, 22, 2, 55, 1, 607450, tzinfo=datetime.timezone.utc), True, 1)]

I don't quite understand what the problem could be. Could you help me?

Hey, @neatek, fastcrud uses timezone aware datetimes. Since from python 3.12 forward this will be the way to go, I decided to adopt it.

Instead of using just :

from datetime import datetime

...

default=datetime.now()

You can pass the desired timezone (using utc):

from datetime import datetime, timezone

...

default=datetime.now(timezone.utc)

If you'd like support for timezone naive datetime, please open a new request, let's see if people want this.

Since from python 3.12 forward this will be the way to go, I decided to adopt it.
If you'd like support for timezone naive datetime, please open a new request, let's see if people want this.

If in 3.12 this is the way to go. That's not necessary. Thank you