Skip to content

Am I able to set a SQL field default value or binding? #1469

Answered by meirdev
configureng asked this question in Questions
Discussion options

You must be logged in to vote

First, datetime.now is not the same as GETUTCDATE(), you should use datetime.utcnow.
Second, to solve your problem you need to add additional arguments to the Column class of the sqlalchemy:

from datetime import datetime
from sqlalchemy import text
from sqlmodel import Field
...
created_at: datetime = Field(default_factory=datetime.utcnow, sa_column_kwargs={"server_default": text("GETUTCDATE()")})

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #404 on August 07, 2025 18:26.