Question: I'm trying to use SQLModel with Fastapi, and on the way I found this example for implementing entities relationships, and I would like to know what does sa_relationship_kwargs={"lazy": "selectin"} means and what does it do? class UserBase(SQLModel): first_name: str last_name: str email: EmailStr = Field(nullable=True, index=True, sa_column_kwargs={"unique": True}) is_active: bool = Field(default=True) is_superuser: bool = Field(default=False) birthdate: Optional[datetime] phone: Optional[str] state: Optional[str] country: Optional[str] address: Optional[str] created_at: Optional[datetime] updated_at: Optional[datetime] class User(UserBase, table=True): id: Optional[int] = Field(default=None, nullable=False, primary_key=True) hashed_password: str = Field( nullable=False, index=True ) role_id: Optional[int] = Field(default=None, foreign_key="role.id") role: Optional["Role"] = Relationship(back_populates="users", sa_relationship_kwargs={"lazy": "selectin"}) groups: List["Group"] = Relationship(back_populates="users", link_model=LinkGroupUser) 

Answer: It chooses the relationship loading technique that SQLAlchemy should use.  The loading of relationships falls into three categories; lazy loading, eager loading, and no loading. Lazy loading refers to objects are returned from a query without the related objects loaded at first. When the given collection or reference is first accessed on a particular object, an additional SELECT statement is emitted such that the requested collection is loaded.  In particular in this case it uses the "select IN loading" technique, which means that a second query will be constructed that loads all child objects through a WHERE parent_id IN (...) construct. Details about the available options:  The primary forms of relationship loading are: lazy loading - available via lazy='select' or the lazyload() option, this is the form of loading that emits a SELECT statement at attribute access time to lazily load a related reference on a single object at a time. Lazy loading is detailed at Lazy Loading. joined loading - available via lazy='joined' or the joinedload() option, this form of loading applies a JOIN to the given SELECT statement so that related rows are loaded in the same result set. Joined eager loading is detailed at Joined Eager Loading. subquery loading - available via lazy='subquery' or the subqueryload() option, this form of loading emits a second SELECT statement which re-states the original query embedded inside of a subquery, then JOINs that subquery to the related table to be loaded to load all members of related collections / scalar references at once. Subquery eager loading is detailed at Subquery Eager Loading. select IN loading - available via lazy='selectin' or the selectinload() option, this form of loading emits a second (or more) SELECT statement which assembles the primary key identifiers of the parent objects into an IN clause, so that all members of related collections / scalar references are loaded at once by primary key. Select IN loading is detailed at Select IN loading. raise loading - available via lazy='raise', lazy='raise_on_sql', or the raiseload() option, this form of loading is triggered at the same time a lazy load would normally occur, except it raises an ORM exception in order to guard against the application making unwanted lazy loads. An introduction to raise loading is at Preventing unwanted lazy loads using raiseload. no loading - available via lazy='noload', or the noload() option; this loading style turns the attribute into an empty attribute (None or []) that will never load or have any loading effect. This seldom-used strategy behaves somewhat like an eager loader when objects are loaded in that an empty attribute or collection is placed, but for expired objects relies upon the default value of the attribute being returned on access; the net effect is the same except for whether or not the attribute name appears in the InstanceState.unloaded collection. noload may be useful for implementing a “write-only” attribute but this usage is not currently tested or formally supported.