我遇到了这个问题,我想要使用knexjs插入一组数据.但我希望插入忽略重复,即只有在数据不存在时才插入.我可以做这个
knex('rates').insert(allRates);
但我想知道ignore如果它存在,我可以使用修改器.我也不想做knex.raw
谢谢.
Ola*_*226 18
2021 年了。现在有一个使用 Knex 的解决方案。
knex('tableName')
.insert({
email: "ignore@example.com",
name: "John Doe"
})
.onConflict('email')
.ignore()
//Outputs: insert ignore into `tableName` (`email`, `name`) values ('ignore@example.com', 'John Doe')
Run Code Online (Sandbox Code Playgroud)
Gab*_*Lee 10
Knex.js不提供此功能.潜在的解决方案是:
knex.raw(knex('rates').insert(allRates).toString().replace('insert', 'INSERT IGNORE'));
Run Code Online (Sandbox Code Playgroud)
它是该raw函数的最小使用量,并不强制您手动转换数据.
| 归档时间: |
|
| 查看次数: |
1741 次 |
| 最近记录: |