File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,10 @@ def pydantic_to_table_schema_columns(
164
164
result [schema_key ] = {
165
165
** hints ,
166
166
"name" : snake_case_naming_convention .make_path (name , hints ["name" ]),
167
+ # if the outer field containing the nested mode is optional,
168
+ # then each field in the model itself has to be nullable as well,
169
+ # as otherwise we end up with flattened non-nullable optional nested fields
170
+ "nullable" : hints ["nullable" ] or nullable ,
167
171
}
168
172
elif data_type == "json" and skip_nested_types :
169
173
continue
Original file line number Diff line number Diff line change @@ -743,3 +743,15 @@ class MyModel(BaseModel):
743
743
744
744
with pytest .raises (ValidationError ):
745
745
m = MyModel (column_type = {"data_type" : "invalid_type" }) # type: ignore[typeddict-item]
746
+
747
+
748
+ def test_parent_nullable_means_children_nullable ():
749
+ class MyParent (BaseModel ):
750
+ optional_child : Optional [ChildModel ]
751
+ non_optional_child : ChildModel
752
+ dlt_config : ClassVar [DltConfig ] = {"skip_nested_types" : True }
753
+
754
+ schema = pydantic_to_table_schema_columns (MyParent )
755
+
756
+ assert schema ["optional_child__child_attribute" ]["nullable" ]
757
+ assert schema ["non_optional_child__child_attribute" ]["nullable" ] is False
You can’t perform that action at this time.
0 commit comments