Add the `createMany` method to SQLite
See original GitHub issueProblem
The createMany
operator is unavailable for SQLite, whilst it is available like this:
INSERT INTO `main`.`sometable` (path1, path2) VALUES
(someValue, otherValue),
(oneMoreValue, lastValue);
The issue is that creating hundreds of rows is extremely slow currently with usual methods, and the reason as to why Prisma exists is not writing SQL manually, but rather as an ORM.
The current solution to this problem is doing this:
const values = someArr
.map(value => `(${value.path1}, ${value.path2})`)
.join(",\n\t")
await prisma.$executeRawUnsafe(`INSERT INTO \`main\`.\`sometable\` (path1, path2) VALUES \n\t${values};`)
This is not ideal due to at least two reasons:
- It uses the
$queryRawUnsafe
method, which can let SQL injections in - even though it’s less risky with custom validation and escaping - It requires custom SQL typing, which is not the goal of Prisma
Suggested solution
Add in the createMany
function for SQLite, as it is working, just like for MySQL. The query can be seen above working
Alternatives
I have typed the raw SQL that makes this work above, both that work with prisma’s method and any SQL runner.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:48
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Prisma Client API (Reference)
createMany. A nested createMany query adds a new set of records to a parent record. See: Working with relations .
Read more >sql - Is it possible to insert multiple rows at a time in an SQLite ...
update. As BrianCampbell points out here, SQLite 3.7.11 and above now supports the simpler syntax of the original post. However, the approach shown...
Read more >SQLite Create Table with Examples
In this tutorial, you will learn how to create a new table using SQLite CREATE TABLE ... To add the table primary key...
Read more >SQLite-Net Extensions - many-to-many relationships
It adds the extension methods/attributes to handle relationships in SQLite database. It doesn't provide any lazy-loading mechanism, instead ...
Read more >“CREATE TABLE IF NOT EXISTS” create many TABLEs - Databases ...
Get the list of tables through the 'Tables' method right after the sql statement. This way, each time a duplicate table is added,...
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
From what I could find, SQLite have different limits for multiple inserts depending on the version https://stackoverflow.com/questions/15858466/limit-on-multiple-rows-insert
But for anyone looking for this only because of speeds, if you put all the inserts in the same transaction it’s MUCH faster https://sqlite.org/faq.html#q19
This can be done with something like
https://www.prisma.io/docs/concepts/components/prisma-client/transactions#the-transaction-api
That is the point of an internal discussion - sorry 😄 We dug up some historic context on how we added
createMany
as a preview feature, then realized it does not work for SQLite because of reasons, then removed it and blocked it - which ended up with the current state when we removed the preview feature and made it GA.We will take that into account when we take another look at this. It is unfortunately not as trivial as it might seem from the outside with the many, many way we need to create queries under the hood.