File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 1- from typing import List , Optional
1+ from decimal import Decimal
2+ from typing import Annotated , List , Optional
23
34import pytest
45from sqlalchemy .exc import IntegrityError
@@ -125,3 +126,25 @@ class Hero(SQLModel, table=True):
125126 # The next statement should not raise an AttributeError
126127 assert hero_rusty_man .team
127128 assert hero_rusty_man .team .name == "Preventers"
129+
130+
131+ def test_optional_annotated_decimal ():
132+ class Model (SQLModel , table = True ):
133+ id : Optional [int ] = Field (default = None , primary_key = True )
134+ dec : Annotated [Decimal , Field (max_digits = 4 , decimal_places = 2 )] | None = None
135+
136+ engine = create_engine ("sqlite://" )
137+
138+ SQLModel .metadata .create_all (engine )
139+
140+ with Session (engine ) as session :
141+ session .add (model := Model (dec = Decimal ("3.14" )))
142+ session .commit ()
143+ session .refresh (model )
144+ assert model .dec == Decimal ("3.14" )
145+
146+ with Session (engine ) as session :
147+ session .add (model := Model (dec = Decimal ("3.142" )))
148+ session .commit ()
149+ session .refresh (model )
150+ assert model .dec == Decimal ("3.14" )
You can’t perform that action at this time.
0 commit comments