Return a copy of this :class:`_schema.Table` associated with a different :class:`_schema.MetaData`. E.g.:: m1 = MetaData() user = Table("user", m1, Column("id", Integer, primary_key=True)) m2 = MetaData() user_copy = user.to_metadat
(
self,
metadata: MetaData,
schema: Union[str, Literal[SchemaConst.RETAIN_SCHEMA]] = RETAIN_SCHEMA,
referred_schema_fn: Optional[
Callable[
[Table, Optional[str], ForeignKeyConstraint, Optional[str]],
Optional[str],
]
] = None,
name: Optional[str] = None,
)
| 1332 | ) |
| 1333 | |
| 1334 | def to_metadata( |
| 1335 | self, |
| 1336 | metadata: MetaData, |
| 1337 | schema: Union[str, Literal[SchemaConst.RETAIN_SCHEMA]] = RETAIN_SCHEMA, |
| 1338 | referred_schema_fn: Optional[ |
| 1339 | Callable[ |
| 1340 | [Table, Optional[str], ForeignKeyConstraint, Optional[str]], |
| 1341 | Optional[str], |
| 1342 | ] |
| 1343 | ] = None, |
| 1344 | name: Optional[str] = None, |
| 1345 | ) -> Table: |
| 1346 | """Return a copy of this :class:`_schema.Table` associated with a |
| 1347 | different :class:`_schema.MetaData`. |
| 1348 | |
| 1349 | E.g.:: |
| 1350 | |
| 1351 | m1 = MetaData() |
| 1352 | |
| 1353 | user = Table("user", m1, Column("id", Integer, primary_key=True)) |
| 1354 | |
| 1355 | m2 = MetaData() |
| 1356 | user_copy = user.to_metadata(m2) |
| 1357 | |
| 1358 | .. versionchanged:: 1.4 The :meth:`_schema.Table.to_metadata` function |
| 1359 | was renamed from :meth:`_schema.Table.tometadata`. |
| 1360 | |
| 1361 | |
| 1362 | :param metadata: Target :class:`_schema.MetaData` object, |
| 1363 | into which the |
| 1364 | new :class:`_schema.Table` object will be created. |
| 1365 | |
| 1366 | :param schema: optional string name indicating the target schema. |
| 1367 | Defaults to the special symbol :attr:`.RETAIN_SCHEMA` which indicates |
| 1368 | that no change to the schema name should be made in the new |
| 1369 | :class:`_schema.Table`. If set to a string name, the new |
| 1370 | :class:`_schema.Table` |
| 1371 | will have this new name as the ``.schema``. If set to ``None``, the |
| 1372 | schema will be set to that of the schema set on the target |
| 1373 | :class:`_schema.MetaData`, which is typically ``None`` as well, |
| 1374 | unless |
| 1375 | set explicitly:: |
| 1376 | |
| 1377 | m2 = MetaData(schema="newschema") |
| 1378 | |
| 1379 | # user_copy_one will have "newschema" as the schema name |
| 1380 | user_copy_one = user.to_metadata(m2, schema=None) |
| 1381 | |
| 1382 | m3 = MetaData() # schema defaults to None |
| 1383 | |
| 1384 | # user_copy_two will have None as the schema name |
| 1385 | user_copy_two = user.to_metadata(m3, schema=None) |
| 1386 | |
| 1387 | :param referred_schema_fn: optional callable which can be supplied |
| 1388 | in order to provide for the schema name that should be assigned |
| 1389 | to the referenced table of a :class:`_schema.ForeignKeyConstraint`. |
| 1390 | The callable accepts this parent :class:`_schema.Table`, the |
| 1391 | target schema that we are changing to, the |