Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert without values #5899

Closed
lueenavarro opened this issue Apr 18, 2020 · 2 comments
Closed

How to insert without values #5899

lueenavarro opened this issue Apr 18, 2020 · 2 comments

Comments

@lueenavarro
Copy link

lueenavarro commented Apr 18, 2020

Issue type:

[ x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

I need to generate the following query using typeorm:

INSERT INTO `invoice_id`(`invoiceId`)
WITH RECURSIVE cte AS
(
  SELECT 1 AS i
  UNION ALL
  SELECT i+1
  FROM cte
  WHERE i < 10
)
SELECT *
FROM cte

I understand that the WITH part is hard / impossible to do in typeorm, but at least I want to do the INSERT INTO `invoice_id`(`invoiceId`) the typeorm way.

The documentation only shows how to do raw queries with values, but I do not use the VALUES keyword here.

This is my initial code:

this.createQueryBuilder()
      .insert()
      .into(InvoiceId)
      .values({
        invoiceId: () => `
        WITH RECURSIVE cte AS
        (
           SELECT 1 AS i
           UNION ALL
           SELECT i+1
           FROM cte
           WHERE i < 10
        )
        SELECT *
        FROM cte`,
      });

which results to this:

INSERT INTO `invoice_id`(`invoiceId`) VALUES (
        WITH RECURSIVE cte AS
        (
           SELECT 1 AS i
           UNION ALL
           SELECT i+1
           FROM cte
           WHERE i < 10
        )
        SELECT *
        FROM cte)

I just want to remove the enclosing VALUES keyword, also I need to replace 1 and 10 with parameters.

@imnotjames
Copy link
Contributor

This isn't possible with the query builder as it exists today. You're best off writing your own queries for something that complex.

@imnotjames
Copy link
Contributor

For questions, please check out the community slack or check TypeORM's documentation page on other support avenues - cheers!

Ginden added a commit to Ginden/typeorm that referenced this issue Jan 15, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Jan 15, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Feb 17, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Feb 17, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Feb 17, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Feb 26, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Mar 7, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Mar 21, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Mar 23, 2022
Ginden added a commit to Ginden/typeorm that referenced this issue Mar 23, 2022
pleerock pushed a commit that referenced this issue Mar 23, 2022
* feat: support for Common Table Expressions

Resolves #1116 #5899

* test: disable test for #4753 if no MySQL is present in ormconfig.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants