MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / from_select

Method from_select

lib/sqlalchemy/sql/dml.py:1276–1328  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

1274
1275 @_generative
1276 def from_select(
1277 self,
1278 names: Sequence[_DMLColumnArgument],
1279 select: Selectable,
1280 include_defaults: bool = True,
1281 ) -> Self:
1282 """Return a new :class:`_expression.Insert` construct which represents
1283 an ``INSERT...FROM SELECT`` statement.
1284
1285 e.g.::
1286
1287 sel = select(table1.c.a, table1.c.b).where(table1.c.c > 5)
1288 ins = table2.insert().from_select(["a", "b"], sel)
1289
1290 :param names: a sequence of string column names or
1291 :class:`_schema.Column`
1292 objects representing the target columns.
1293 :param select: a :func:`_expression.select` construct,
1294 :class:`_expression.FromClause`
1295 or other construct which resolves into a
1296 :class:`_expression.FromClause`,
1297 such as an ORM :class:`_query.Query` object, etc. The order of
1298 columns returned from this FROM clause should correspond to the
1299 order of columns sent as the ``names`` parameter; while this
1300 is not checked before passing along to the database, the database
1301 would normally raise an exception if these column lists don't
1302 correspond.
1303 :param include_defaults: if True, non-server default values and
1304 SQL expressions as specified on :class:`_schema.Column` objects
1305 (as documented in :ref:`metadata_defaults_toplevel`) not
1306 otherwise specified in the list of names will be rendered
1307 into the INSERT and SELECT statements, so that these values are also
1308 included in the data to be inserted.
1309
1310 .. note:: A Python-side default that uses a Python callable function
1311 will only be invoked **once** for the whole statement, and **not
1312 per row**.
1313
1314 """
1315
1316 if self._values:
1317 raise exc.InvalidRequestError(
1318 "This construct already inserts value expressions"
1319 )
1320
1321 self._select_names = [
1322 coercions.expect(roles.DMLColumnRole, name, as_key=True)
1323 for name in names
1324 ]
1325 self._inline = True
1326 self.include_insert_from_select_defaults = include_defaults
1327 self.select = coercions.expect(roles.DMLSelectRole, select)
1328 return self
1329
1330 if TYPE_CHECKING:
1331 # START OVERLOADED FUNCTIONS self.returning ReturningInsert 1-8 ", *, sort_by_parameter_order: bool = False" # noqa: E501

Calls

no outgoing calls