-
-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dose there any better way to write timezone aware datetime field without using the SQLAlchemy ? #539
Comments
Generally you can't do this kind of things with SQLModel without using SA api directly, so I'd just drop that want. |
Coming from Tortoise ORM moving to SQLModel it would be great to have if the timezone would be saved without having to use SQLAlchemy directly. The project is new so I hope updates could be made soon. |
New release has
so maybe this could be extended somehow to allow arguments for the sa Column type too. |
That's how I sort this out now: from typing import Annotated
from datetime import datetime, timezone
import pytest
from pydantic import types as pydantic_types, ValidationError
from sqlmodel import Field, SQLModel, create_engine, Session, DateTime, Column
class DummySchema(SQLModel):
timestamp: pydantic_types.AwareDatetime = Field(sa_type=DateTime(timezone=True))
class DummyModel(DummySchema, table=True):
id: int | None = Field(default=None, primary_key=True) Indeed @antont , I believe adding That will throw an error: class DummySchema(SQLModel):
timestamp: pydantic_types.AwareDatetime = Field() while that works:
Just considering all the
as well as an updated Taking a step back, I believe the this kind of challenges quite common, and making type-mapping used in In the end one could: I can dig into contributing to this, yet I'd like to know if there any kind of interest for such a contrib'? |
First Check
Commit to Help
Example Code
Description
I write my
created_at
andupdated_at
fields like this, however, this did not work, because of the time awareness,After checking Github, i found this solution:
It is written with mixing SQLModel stuff and the SALAlchemy, I know SQLModel is SQLAlchemy under the hood but this feels strange, cause i want to face SQLModel ONLY.
Is there any better way of handling this?
Let's say when SQLModel create tables, it will check the payding field
created_at
, if it is timezone aware datetime then it will set it assa_column=sa.Column(sa.DateTime(timezone=True)
so that we do not need to mix them both,Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.8
Python Version
3.10.2
Additional Context
No response
The text was updated successfully, but these errors were encountered: