Skip to content

Allow INSERT ... SELECT FROM #8538

Closed

Description

@Ginden

Feature Description

Allow InsertQueryBuilder to use SELECT.

The Problem

Following code can't be generated using QueryBuilder

INSERT INTO foo(name)
SELECT "name" FROM other_table

The Solution

Obvious solution would be to use something like:

connection.getRepository(foo)
.insert()
.from(OtherTable, 'other_table')
.innerJoin(YetAnotherTable, 'yet_another_table', 'other_table ... = yet_another_table ...')
.where('...')

Easier to implement would be:

class InsertQueryBuilder<T = any> {
  from(queryBuilder: QueryBuilder<any>, alias?: string, columnNames?: (keyof T)[]);
}

and generate query like

INSERT INTO foo(name)
SELECT alias.name
FROM (
SELECT "name" FROM other_table
) alias

Considered Alternatives

Use other query builder library of construct query manually.

Additional Context

https://www.postgresql.org/docs/14/sql-insert.html

(redacted for clarity)

INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ]
    { query }

Relevant Database Driver(s)

DB Type Relevant
aurora-data-api yes
aurora-data-api-pg yes
better-sqlite3 yes
cockroachdb yes
cordova yes
expo yes
mongodb yes
mysql yes
nativescript yes
oracle yes
postgres yes
react-native yes
sap yes
sqlite yes
sqlite-abstract yes
sqljs yes
sqlserver yes

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. mortezaPRK commented on Aug 17, 2022

    @mortezaPRK
    Contributor

    @Ginden I need this feature and also the one mentioned in #8542.

    Do you plan to create a PR for it? if not, I'm ready to it.

  2. Ginden commented on Aug 17, 2022

    @Ginden
    ContributorAuthor

    Do you plan to create a PR for it? if not, I'm ready to it.

    You will probably be faster, because I will be busy at least until end of September.

  3. PurpleTape commented on Feb 4, 2024

    @PurpleTape

    Any updates? 🥺🥺🥺

  4. rohitkhatri commented on Jul 10, 2024

    @rohitkhatri

    Any update on this?

  5. VMolokostovEnergiAI commented on Nov 7, 2024

    @VMolokostovEnergiAI

    Anything so far?

  6. Ginden commented on Nov 7, 2024

    @Ginden
    ContributorAuthor

    None, I'm not at company using TypeORM anymore.

  7. JTBrinkmann commented on Jan 3, 2025

    @JTBrinkmann

    this duplicates #4374

  8. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

  9. OverWeo commented on Nov 13, 2025

    @OverWeo

    Any updates?

  10. reopened this on Nov 13, 2025
  11. Moustafa3attia commented on Mar 16, 2026

    @Moustafa3attia

    Still relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions