Return a new :class:`_expression.Insert` construct which represents an ``INSERT...FROM SELECT`` statement. e.g.:: sel = select(table1.c.a, table1.c.b).where(table1.c.c > 5) ins = table2.insert().from_select(["a", "b"], sel) :param names: a sequence
(
self,
names: Sequence[_DMLColumnArgument],
select: Selectable,
include_defaults: bool = True,
)
| 1314 | |
| 1315 | @_generative |
| 1316 | def from_select( |
| 1317 | self, |
| 1318 | names: Sequence[_DMLColumnArgument], |
| 1319 | select: Selectable, |
| 1320 | include_defaults: bool = True, |
| 1321 | ) -> Self: |
| 1322 | """Return a new :class:`_expression.Insert` construct which represents |
| 1323 | an ``INSERT...FROM SELECT`` statement. |
| 1324 | |
| 1325 | e.g.:: |
| 1326 | |
| 1327 | sel = select(table1.c.a, table1.c.b).where(table1.c.c > 5) |
| 1328 | ins = table2.insert().from_select(["a", "b"], sel) |
| 1329 | |
| 1330 | :param names: a sequence of string column names or |
| 1331 | :class:`_schema.Column` |
| 1332 | objects representing the target columns. |
| 1333 | :param select: a :func:`_expression.select` construct, |
| 1334 | :class:`_expression.FromClause` |
| 1335 | or other construct which resolves into a |
| 1336 | :class:`_expression.FromClause`, |
| 1337 | such as an ORM :class:`_query.Query` object, etc. The order of |
| 1338 | columns returned from this FROM clause should correspond to the |
| 1339 | order of columns sent as the ``names`` parameter; while this |
| 1340 | is not checked before passing along to the database, the database |
| 1341 | would normally raise an exception if these column lists don't |
| 1342 | correspond. |
| 1343 | :param include_defaults: if True, non-server default values and |
| 1344 | SQL expressions as specified on :class:`_schema.Column` objects |
| 1345 | (as documented in :ref:`metadata_defaults_toplevel`) not |
| 1346 | otherwise specified in the list of names will be rendered |
| 1347 | into the INSERT and SELECT statements, so that these values are also |
| 1348 | included in the data to be inserted. |
| 1349 | |
| 1350 | .. note:: A Python-side default that uses a Python callable function |
| 1351 | will only be invoked **once** for the whole statement, and **not |
| 1352 | per row**. |
| 1353 | |
| 1354 | """ |
| 1355 | |
| 1356 | if self._values: |
| 1357 | raise exc.InvalidRequestError( |
| 1358 | "This construct already inserts value expressions" |
| 1359 | ) |
| 1360 | |
| 1361 | self._select_names = [ |
| 1362 | coercions.expect(roles.DMLColumnRole, name, as_key=True) |
| 1363 | for name in names |
| 1364 | ] |
| 1365 | self._inline = True |
| 1366 | self.include_insert_from_select_defaults = include_defaults |
| 1367 | self.select = coercions.expect(roles.DMLSelectRole, select) |
| 1368 | return self |
| 1369 | |
| 1370 | if TYPE_CHECKING: |
| 1371 | # START OVERLOADED FUNCTIONS self.returning ReturningInsert 1-8 ", *, sort_by_parameter_order: bool = False" # noqa: E501 |
no outgoing calls