Skip to content

Default values for fields not working as expected with SQLModel in PostgreSQL #1452

Answered by ArazHeydarov
Yoosuph asked this question in Questions
Discussion options

You must be logged in to vote

default and default_factory arguments are only Python-level defaults. They'll assign a default value to your field only during the creation of instances in Python.

post = Posts(title="Some title", content="Some Content")
assert post.id == 0

If you go directly to the Postgres DB and try to add a row there, these defaults won't be considered.
In order to enforce the default in DB level, you can use sa_column_kwargs={"server_default": "<value>"} argument.
I hope this will solve your issue.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1260 on August 06, 2025 15:09.