'sa_relationship_kwargs={"lazy": "selectin"}' 在带有 Fastapi 的 SQLModel 上意味着什么?
What does 'sa_relationship_kwargs={"lazy": "selectin"}' means on SQLModel with Fastapi?
我正在尝试将 SQLModel 与 Fastapi 一起使用,在途中我发现了这个用于实现实体关系的示例,我想知道 sa_relationship_kwargs={"lazy": "selectin"}
是什么意思,它有什么作用?
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)
选择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.
特别是在这种情况下,它使用“select IN 加载”技术,这意味着将构建第二个查询,通过 WHERE parent_id IN (...)
构造加载所有子对象。 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.
我正在尝试将 SQLModel 与 Fastapi 一起使用,在途中我发现了这个用于实现实体关系的示例,我想知道 sa_relationship_kwargs={"lazy": "selectin"}
是什么意思,它有什么作用?
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)
选择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.
特别是在这种情况下,它使用“select IN 加载”技术,这意味着将构建第二个查询,通过 WHERE parent_id IN (...)
构造加载所有子对象。 Details about the available options:
The primary forms of relationship loading are:
lazy loading - available via
lazy='select'
or thelazyload()
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 thejoinedload()
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 thesubqueryload()
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 theselectinload()
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 thenoload()
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.