发布:2.0.36 当前发布 | 发布日期:2024 年 10 月 15 日
SQLAlchemy 2.0 文档
- 上级: 首页
- 本页内容
ORM 查询指南设置:SELECT¶
本页说明了为 ORM 映射类编写 SELECT 语句 文档中使用的映射和测试数据,该文档来自ORM 查询指南。
>>> from typing import List
>>> from typing import Optional
>>>
>>> from sqlalchemy import Column
>>> from sqlalchemy import create_engine
>>> from sqlalchemy import ForeignKey
>>> from sqlalchemy import Table
>>> from sqlalchemy.orm import DeclarativeBase
>>> from sqlalchemy.orm import Mapped
>>> from sqlalchemy.orm import mapped_column
>>> from sqlalchemy.orm import relationship
>>> from sqlalchemy.orm import Session
>>>
>>>
>>> class Base(DeclarativeBase):
... pass
>>> class User(Base):
... __tablename__ = "user_account"
... id: Mapped[int] = mapped_column(primary_key=True)
... name: Mapped[str]
... fullname: Mapped[Optional[str]]
... addresses: Mapped[List["Address"]] = relationship(back_populates="user")
... orders: Mapped[List["Order"]] = relationship()
...
... def __repr__(self) -> str:
... return f"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})"
>>> class Address(Base):
... __tablename__ = "address"
... id: Mapped[int] = mapped_column(primary_key=True)
... user_id: Mapped[int] = mapped_column(ForeignKey("user_account.id"))
... email_address: Mapped[str]
... user: Mapped[User] = relationship(back_populates="addresses")
...
... def __repr__(self) -> str:
... return f"Address(id={self.id!r}, email_address={self.email_address!r})"
>>> order_items_table = Table(
... "order_items",
... Base.metadata,
... Column("order_id", ForeignKey("user_order.id"), primary_key=True),
... Column("item_id", ForeignKey("item.id"), primary_key=True),
... )
>>>
>>> class Order(Base):
... __tablename__ = "user_order"
... id: Mapped[int] = mapped_column(primary_key=True)
... user_id: Mapped[int] = mapped_column(ForeignKey("user_account.id"))
... items: Mapped[List["Item"]] = relationship(secondary=order_items_table)
>>> class Item(Base):
... __tablename__ = "item"
... id: Mapped[int] = mapped_column(primary_key=True)
... name: Mapped[str]
... description: Mapped[str]
>>> engine = create_engine("sqlite+pysqlite:///:memory:", echo=True)
>>> Base.metadata.create_all(engine)
BEGIN ...
>>> conn = engine.connect()
>>> session = Session(conn)
>>> session.add_all(
... [
... User(
... name="spongebob",
... fullname="Spongebob Squarepants",
... addresses=[Address(email_address="[email protected]")],
... ),
... User(
... name="sandy",
... fullname="Sandy Cheeks",
... addresses=[
... Address(email_address="[email protected]"),
... Address(email_address="[email protected]"),
... ],
... ),
... User(
... name="patrick",
... fullname="Patrick Star",
... addresses=[Address(email_address="[email protected]")],
... ),
... User(
... name="squidward",
... fullname="Squidward Tentacles",
... addresses=[Address(email_address="[email protected]")],
... ),
... User(name="ehkrabs", fullname="Eugene H. Krabs"),
... ]
... )
>>> session.commit()
BEGIN ... COMMIT
>>> conn.begin()
BEGIN ...
© 版权 2007-2024,SQLAlchemy 作者和贡献者。
flambé! 龙和炼金术士图像设计由 Rotem Yaari 创建并慷慨捐赠。
使用 Sphinx 7.2.6 创建。文档最后生成时间:2024 年 11 月 8 日星期五上午 08:41:19 EST