r"""Represent a table in a database. e.g.:: from sqlalchemy import Table, MetaData, Integer, String, Column metadata = MetaData() mytable = Table( "mytable", metadata, Column("mytable_id", Integer, primary_key=True), Col
| 326 | |
| 327 | |
| 328 | class Table( |
| 329 | DialectKWArgs, |
| 330 | HasSchemaAttr, |
| 331 | TableClause[_ColCC_co], |
| 332 | inspection.Inspectable[class="st">"Table"], |
| 333 | ): |
| 334 | rclass="st">"""Represent a table in a database. |
| 335 | |
| 336 | e.g.:: |
| 337 | |
| 338 | from sqlalchemy import Table, MetaData, Integer, String, Column |
| 339 | |
| 340 | metadata = MetaData() |
| 341 | |
| 342 | mytable = Table( |
| 343 | class="st">"mytable", |
| 344 | metadata, |
| 345 | Column(class="st">"mytable_id", Integer, primary_key=True), |
| 346 | Column(class="st">"value", String(50)), |
| 347 | ) |
| 348 | |
| 349 | The :class:`_schema.Table` |
| 350 | object constructs a unique instance of itself based |
| 351 | on its name and optional schema name within the given |
| 352 | :class:`_schema.MetaData` object. Calling the :class:`_schema.Table` |
| 353 | constructor with the same name and same :class:`_schema.MetaData` argument |
| 354 | a second time will return the *same* :class:`_schema.Table` |
| 355 | object - in this way |
| 356 | the :class:`_schema.Table` constructor acts as a registry function. |
| 357 | |
| 358 | May also be defined as class="st">"typed table" by passing a subclass of |
| 359 | :class:`_schema.TypedColumns` as the 3rd argument:: |
| 360 | |
| 361 | from sqlalchemy import TypedColumns, select |
| 362 | |
| 363 | |
| 364 | class user_cols(TypedColumns): |
| 365 | id = Column(Integer, primary_key=True) |
| 366 | name: Column[str] |
| 367 | age: Column[int] |
| 368 | middle_name: Column[str | None] |
| 369 | |
| 370 | class="cm"># optional, used to infer the select types when selecting the table |
| 371 | __row_pos__: tuple[int, str, int, str | None] |
| 372 | |
| 373 | |
| 374 | user = Table(class="st">"user", metadata, user_cols) |
| 375 | |
| 376 | class="cm"># the columns are typed: the statement has type Select[int, str] |
| 377 | stmt = sa.select(user.c.id, user.c.name).where(user.c.age > 30) |
| 378 | |
| 379 | class="cm"># Inferred as Select[int, str, int, str | None] thanks to __row_pos__ |
| 380 | stmt1 = user.select() |
| 381 | stmt2 = sa.select(user) |
| 382 | |
| 383 | The :attr:`sqlalchemy.sql._annotated_cols.HasRowPos.__row_pos__` |
| 384 | annotation is optional, and it&class="cm">#x27;s used to infer the types in a |
| 385 | :class:`_sql.Select` when selecting the complete table. |
no outgoing calls