How to do multiple inserts
See original GitHub issueThis is kind of a noob question, I’m used to using ORMs and decided to go closer to the metal with Knex.
I’m trying to do multiple inserts where the 2nd insert relies on the id
of the 1st insert.
Something like this:
knex("users")
.insert({ first_name: "John", last_name: "Doe" })
.exec(function (err, id) {
knex("groups")
.insert({ name: "Cool Group" })
.exec(function (err, id) {
// handle results
});
});
I might be missing something by not knowing how to use Promises or Transactions properly, but I’d appreciate some help on this very simple problem.
Thanks!
Issue Analytics
- State:
- Created 9 years ago
- Comments:17 (11 by maintainers)
Top Results From Across the Web
SQL Server INSERT Multiple Rows Into a Table Using One ...
To insert multiple rows returned from a SELECT statement, you use the INSERT INTO SELECT statement. SQL Server INSERT multiple rows – examples....
Read more >Inserting multiple rows in a single SQL query? - Stack Overflow
INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists ...
Read more >SQL Query to Insert Multiple Rows - GeeksforGeeks
In this article, we see how to insert individual as well as multiple rows in a database using the INSERT statement in the...
Read more >How to INSERT Multiple Records in SQL - DigitalOcean
SQL INSERT query inserts data into the columns of a particular table. The normal SQL INSERT query inputs the data values in a...
Read more >How to Insert Multiple Rows in SQL - Database Star
If you have your data in another table and want to insert it into a new table, you can use an INSERT statement...
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
With transactions:
For anyone on the internet who comes across this issue like I did
you can read this SO answer.
Basically in your
up
ordown
handler you need to return apromise
so technically you could do something like: