You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced/sa-column.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,3 +31,46 @@ The `updated_at` column has an `onupdate` value of `func.now()`, this means that
31
31
```
32
32
33
33
</details>
34
+
35
+
### Pydantic Implementation
36
+
37
+
Implementing these timestamps on the DB side with SQLAlchemy works very well as the database itself is what will create and update the fields whenever a relevant database interaction occurs.
38
+
39
+
It's possible to achieve similar behaviour with Pydantic, for the `created_at` timestamp by using a Pydantic `Field` with a `default_factory`:
Another approach is to use a Pydantic `validator`:
58
+
59
+
```python
60
+
from datetime import datetime
61
+
62
+
from pydantic import BaseModel, validator
63
+
64
+
classModel(BaseModel):
65
+
created_at: datetime =None
66
+
67
+
@validator('ts', pre=True, always=True)
68
+
defset_created_at_now(cls, v):
69
+
return v or datetime.now()
70
+
```
71
+
72
+
Both of these approaches come with the major caveat that default fields are set during the **Pydantic model instantiation**, as opposed to during **interactions with the database**, instead of the SQLModel approach which sets it with `server_default` which means that the timestamp will be exactly when the row is created in the database.
73
+
74
+
The real issue starts when looking at the `updated_at` timestamp - SQLAlchemy has the `onupdate` default which runs a function when the row is updated in the database, but there is no easy way to do this in Pydantic as it has no concept of 'about to be saved'.
75
+
76
+
So the pure Pydantic approach would require some additional logic to always change the `updated_at` timestamp before doing a write to the database, which adds some more complexity to the code and does not have benefits over the SQLAlchemy approach.
0 commit comments