Skip to content

Commit f60faf9

Browse files
authored
Merge pull request #59 from GabrielVGS/dev
Dev
2 parents 9858b73 + 0f69a5d commit f60faf9

3 files changed

Lines changed: 30 additions & 23 deletions

File tree

fastapi-base/src/models/base.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sqlalchemy import text
77
from sqlalchemy.ext.compiler import compiles
88
from sqlalchemy.sql import expression
9-
from sqlmodel import Column, DateTime, Field, SQLModel
9+
from sqlmodel import DateTime, Field, SQLModel
1010

1111

1212
# https://docs.sqlalchemy.org/en/20/core/compiler.html#utc-timestamp-function
@@ -20,8 +20,17 @@ def pg_utcnow(element, compiler, **kw) -> str: # type: ignore
2020
return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
2121

2222

23+
# def utc_column(server_default=False, onupdate=False):
24+
# return Column(
25+
# DateTime(timezone=True),
26+
# server_default=utcnow() if server_default else None,
27+
# onupdate=utcnow() if onupdate else None,
28+
# nullable=True,
29+
# )
30+
31+
2332
# this is the base model, as a best practice, other db models should inherit it
24-
class BaseModel(SQLModel):
33+
class BaseModel(SQLModel, table=False):
2534
id: Optional[uuid.UUID] = Field(
2635
default_factory=uuid_ext_pkg.uuid7,
2736
primary_key=True,
@@ -31,25 +40,22 @@ class BaseModel(SQLModel):
3140

3241
created_at: Optional[datetime] = Field(
3342
default_factory=lambda: datetime.now(timezone.utc),
34-
sa_column=Column(
35-
DateTime(timezone=True),
36-
server_default=utcnow(),
37-
nullable=True,
38-
),
43+
sa_type=DateTime(timezone=True), # type: ignore
44+
sa_column_kwargs={
45+
"server_default": text("TIMEZONE('utc', CURRENT_TIMESTAMP)"),
46+
"nullable": True,
47+
},
3948
)
4049
updated_at: Optional[datetime] = Field(
4150
default_factory=lambda: datetime.now(timezone.utc),
42-
sa_column=Column(
43-
DateTime(timezone=True),
44-
onupdate=utcnow(),
45-
nullable=True,
46-
),
51+
sa_type=DateTime(timezone=True), # type: ignore
52+
sa_column_kwargs={
53+
"onupdate": text("TIMEZONE('utc', CURRENT_TIMESTAMP)"),
54+
"nullable": True,
55+
},
4756
)
48-
4957
deleted_at: Optional[datetime] = Field(
5058
default=None,
51-
sa_column=Column(
52-
DateTime(timezone=True),
53-
nullable=True,
54-
),
59+
sa_type=DateTime(timezone=True), # type: ignore
60+
sa_column_kwargs={"nullable": True},
5561
)

fastapi-base/src/schemas/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pydantic import BaseModel, Field
44

5+
56
T = TypeVar("T")
67

78

fastapi-base/uv.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)