Reputation: 162
My code for inserting multiple entries in the database is like this:
let model = [
{
Question: '1 + 1',
QuestionTypeId: 1,
Answer: '2',
QuizId: 1,
Options: null
},
{
Question: '1 + 2',
QuestionTypeId: 1,
Answer: '3',
QuizId: 1,
Options: null
}
]
let result = knex('Items').insert(model)
but it inserts the data twice on the database like this
Can somebody explain to me why it inserts 2 more rows on the database? Thanks!
Upvotes: 0
Views: 378
Reputation: 19718
The code you pasted in question actually does not insert anything (query builder is never triggered).
Knex does not insert the data twice unless you tell it to insert it twice. Maybe in the code that you have not show in question you are calling .then()
twice for the query builder stored in variable result
.
This may work better:
let model = [
{
Question: '1 + 1',
QuestionTypeId: 1,
Answer: '2',
QuizId: 1,
Options: null
},
{
Question: '1 + 2',
QuestionTypeId: 1,
Answer: '3',
QuizId: 1,
Options: null
}
];
let result = await knex('Items').insert(model);
Upvotes: 2