Skip to content
Discussion options

You must be logged in to vote

That default 1 isn't coming from Pydantic, it's a SQLAlchemy column default. Field(default=1) attaches a ScalarElementColumnDefault(1) to the column, and a scalar column default gets applied on INSERT whenever the value is None, so your explicit None gets replaced with 1. There's no column default on UPDATE (that's onupdate), which is why setting it to None and committing again keeps the null.

To make unset give 1 but explicit None give NULL, move the default off the column so only the Pydantic side remains:

from sqlalchemy import Column, Integer
field: int | None = Field(default=1, sa_column=Column(Integer, nullable=True))

Keep default=1 on the Field, that's now what fills 1 when the att…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@BassMastaCod
Comment options

Answer selected by BassMastaCod
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