Represent a CREATE TABLE ... AS statement. This creates a new table directly from the output of a SELECT, including its schema and its initial set of data. Unlike a view, the new table is fixed and does not synchronize further with the originating SELECT statement. The exampl
| 650 | |
| 651 | |
| 652 | class CreateTableAs(DialectKWArgs, _TableViaSelect): |
| 653 | """Represent a CREATE TABLE ... AS statement. |
| 654 | |
| 655 | This creates a new table directly from the output of a SELECT, including |
| 656 | its schema and its initial set of data. Unlike a view, the |
| 657 | new table is fixed and does not synchronize further with the originating |
| 658 | SELECT statement. |
| 659 | |
| 660 | The example below illustrates basic use of :class:`.CreateTableAs`; given a |
| 661 | :class:`.Select` and optional :class:`.MetaData`, the |
| 662 | :class:`.CreateTableAs` may be invoked directly via |
| 663 | :meth:`.Connection.execute` or indirectly via :meth:`.MetaData.create_all`; |
| 664 | the :attr:`.CreateTableAs.table` attribute provides a :class:`.Table` |
| 665 | object with which to generate new queries:: |
| 666 | |
| 667 | from sqlalchemy import CreateTableAs |
| 668 | from sqlalchemy import select |
| 669 | |
| 670 | # instantiate CreateTableAs given a select() and optional MetaData |
| 671 | cas = CreateTableAs( |
| 672 | select(users.c.id, users.c.name).where(users.c.status == "active"), |
| 673 | "active_users", |
| 674 | metadata=some_metadata, |
| 675 | ) |
| 676 | |
| 677 | # a Table object is available immediately via the .table attribute |
| 678 | new_statement = select(cas.table) |
| 679 | |
| 680 | # to emit CREATE TABLE AS, either invoke CreateTableAs directly... |
| 681 | with engine.begin() as conn: |
| 682 | conn.execute(cas) |
| 683 | |
| 684 | # or alternatively, invoke metadata.create_all() |
| 685 | some_metdata.create_all(engine) |
| 686 | |
| 687 | # drop is performed in the usual way, via drop_all |
| 688 | # or table.drop() |
| 689 | some_metdata.drop_all(engine) |
| 690 | |
| 691 | For detailed background on :class:`.CreateTableAs` see |
| 692 | :ref:`metadata_create_table_as`. |
| 693 | |
| 694 | .. versionadded:: 2.1 |
| 695 | |
| 696 | :param selectable: :class:`_sql.Select` |
| 697 | The SELECT statement providing the columns and rows. |
| 698 | |
| 699 | :param table_name: table name as a string. Combine with the optional |
| 700 | :paramref:`.CreateTableAs.schema` parameter to indicate a |
| 701 | schema-qualified table name. |
| 702 | |
| 703 | :param metadata: :class:`_schema.MetaData`, optional |
| 704 | If provided, the :class:`_schema.Table` object available via the |
| 705 | :attr:`.table` attribute will be associated with this |
| 706 | :class:`.MetaData`. Otherwise, a new, empty :class:`.MetaData` |
| 707 | is created. |
| 708 | |
| 709 | :param schema: str, optional schema or owner name. |
no outgoing calls