SQL syntax error occurs by typeorm insert of multiple rows into table for sap (HANA) database
See original GitHub issueIssue Description
Expected Behavior
Two new records have been created in table "<HANA_SCHEMA>"."photo"
.
Actual Behavior
An error occurs at typeorm database operation. No records have been created on database. Server console output:
[Nest] 1032 - 07/13/2021, 8:27:38 AM ERROR [ExceptionsHandler] sql syntax error: incorrect syntax near ",": line 1 col 48 (at pos 48)
Error: sql syntax error: incorrect syntax near ",": line 1 col 48 (at pos 48)
at SapQueryRunner.<anonymous> (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/src/driver/sap/SapQueryRunner.ts:193:53)
at step (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:143:27)
at Object.next (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:124:57)
at fulfilled (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:114:62)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Steps to Reproduce
Prepare:
- Clone repository https://github.com/niels-schmitt/typeorm-hana-insert-multiple-rows.
npm install
- Get a running HANA instance, and credentials for user <HANA_USER> with authorization
CREATE ANY
on schema <HANA_SCHEMA>. cp template.env .env
- In file
.env
enter values for:HANA_HOST
,HANA_PORT
,HANA_USER
,HANA_PASSWORD
,HANA_SCHEMA
Reproduce Issue:
npm run start
curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower","Tree"]' localhost:3000/photos
or
curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower","Tree"]' localhost:3000/photosByQueryBuilder
My Environment
Dependency | Version |
---|---|
Operating System | Ubuntu 20.04.2 |
Node.js version | v14.17.2 |
Typescript version | @4.3.5 |
TypeORM version | @0.2.34 |
@nestjs/core | @8.0.2 |
@nestjs/typeorm | @8.0.1 |
@sap/hana-client | @2.9.23 |
hdb-pool | @0.1.6 |
Additional Context
Root cause seems to be that an incorrect SQL is created:
INSERT INTO "photo"("id", "name") VALUES (?, ?), (?, ?)
Correct SQL would be:
INSERT INTO "photo"("id", "name") VALUES ((?, ?), (?, ?))
The executed SQL is logged to the console in case of POST for /photosByQueryBuilder
.
POST for /photos
executes
await this.photoRepository.manager.insert(Photo, photos);
POST for /photosByQueryBuilder
executes
await this.photoRepository
.createQueryBuilder()
.insert()
.into(Photo)
.values(photos)
.execute();
The issues occurs for both variants.
The issue does not occur in case only one record is inserted:
curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower"]' localhost:3000/photos
or
curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Tree"]' localhost:3000/photosByQueryBuilder
For verification, read all records by
curl -w'\n' localhost:3000/photos
The issue does not occur in case a postgres database is connected.
Relevant Database Driver(s)
-
aurora-data-api
-
aurora-data-api-pg
-
better-sqlite3
-
cockroachdb
-
cordova
-
expo
-
mongodb
-
mysql
-
nativescript
-
oracle
-
postgres
-
react-native
-
sap
-
sqlite
-
sqlite-abstract
-
sqljs
-
sqlserver
Thanks for your help & kind regards Niels Schmitt
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
SAP HANA Driver wrongly escapes the entity / table name
A Database syntax error is thrown and looks like this: query: INSERT INTO "foo"."bar"."bak::XZY" { code: 257, message: 'sql syntax error: ...
Read more >Inserting Multiple Rows of Data With One Insert in HANA
Symptom. When inserting multiple rows of data with one insert statement by simply comma separating the VALUES part of the statement, it returns...
Read more >Multiple Row Insert into HANA - SAP Community
I'm trying to insert more than a single row into a table I have in HANA. insert into TABLE ("NAME", "VALUE") values ('hi',...
Read more >HANA - Insert Multiple rows of data - SAP Community
I am pulling data from system B(Source) into System A (Target). HANA Native SQL Reading data from Calculation View in System B.
Read more >Insert all default values - SAP Community
Hi, I'm trying to add SAP Hana database support to my ORM. There are cases when entity / table is defined with all...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We do something similar for Oracle.
I think that’s possible for Hana.
Hi James, I have tested draft PR https://github.com/typeorm/typeorm/pull/7957 in my local setup. It looks quite promising, though in case of multiple rows there still exists an SQL-syntax error: The VALUES keyword must be removed for that case. E.g. the SQL query generated by your draft PR looks as follows:
The correct SQL would be:
Therefore, in src/query-builder/InsertQueryBuilder.ts, starting from line 341
line 343 should be modified as follows:
Kind regards, Niels