@@ -199,14 +199,13 @@ def Relationship(
199199 sa_relationship_args : Optional [Sequence [Any ]] = None ,
200200 sa_relationship_kwargs : Optional [Mapping [str , Any ]] = None ,
201201) -> Any :
202- relationship_info = RelationshipInfo (
202+ return RelationshipInfo (
203203 back_populates = back_populates ,
204204 link_model = link_model ,
205205 sa_relationship = sa_relationship ,
206206 sa_relationship_args = sa_relationship_args ,
207207 sa_relationship_kwargs = sa_relationship_kwargs ,
208208 )
209- return relationship_info
210209
211210
212211@__dataclass_transform__ (kw_only_default = True , field_descriptors = (Field , FieldInfo ))
@@ -511,9 +510,8 @@ def __setattr__(self, name: str, value: Any) -> None:
511510 return
512511 else :
513512 # Set in SQLAlchemy, before Pydantic to trigger events and updates
514- if getattr (self .__config__ , "table" , False ):
515- if is_instrumented (self , name ):
516- set_attribute (self , name , value )
513+ if getattr (self .__config__ , "table" , False ) and is_instrumented (self , name ):
514+ set_attribute (self , name , value )
517515 # Set in Pydantic model to trigger possible validation changes, only for
518516 # non relationship values
519517 if name not in self .__sqlmodel_relationships__ :
@@ -531,13 +529,9 @@ def from_orm(cls: Type["SQLModel"], obj: Any, update: Dict[str, Any] = None):
531529 if update is not None :
532530 obj = {** obj , ** update }
533531 # End SQLModel support dict
534- if not getattr (cls .__config__ , "table" , False ):
535- # If not table, normal Pydantic code
536- m = cls .__new__ (cls )
537- else :
538- # If table, create the new instance normally to make SQLAlchemy create
539- # the _sa_instance_state attribute
540- m = cls ()
532+ m = cls .__new__ (cls ) if not getattr (cls .__config__ , "table" , False ) else cls ()
533+ # If table, create the new instance normally to make SQLAlchemy create
534+ # the _sa_instance_state attribute
541535 values , fields_set , validation_error = validate_model (cls , obj )
542536 if validation_error :
543537 raise validation_error
@@ -601,7 +595,7 @@ def _calculate_keys( # type: ignore
601595 exclude_unset : bool ,
602596 update : Optional [Dict [str , Any ]] = None ,
603597 ) -> Optional [AbstractSet [str ]]:
604- if include is None and exclude is None and exclude_unset is False :
598+ if include is None and exclude is None and not exclude_unset :
605599 # Original in Pydantic:
606600 # return None
607601 # Updated to not return SQLAlchemy attributes
@@ -610,16 +604,7 @@ def _calculate_keys( # type: ignore
610604 return self .__fields__ .keys () # | self.__sqlmodel_relationships__.keys()
611605
612606 keys : AbstractSet [str ]
613- if exclude_unset :
614- keys = self .__fields_set__ .copy ()
615- else :
616- # Original in Pydantic:
617- # keys = self.__dict__.keys()
618- # Updated to not return SQLAlchemy attributes
619- # Do not include relationships as that would easily lead to infinite
620- # recursion, or traversing the whole database
621- keys = self .__fields__ .keys () # | self.__sqlmodel_relationships__.keys()
622-
607+ keys = self .__fields_set__ .copy () if exclude_unset else self .__fields__ .keys ()
623608 if include is not None :
624609 keys &= include .keys ()
625610
0 commit comments