javascript - 使用 knexjs 检测并发出 onConflict 信号
问题描述
此处的此文档解释了使用onConflict()
andignore()
将简单地安静地放弃该操作。如果我试图将新行插入到表中,如下所示:
const addItem = (item) => {
return database(dbTable).insert(item)
.onConflict('name')
.ignore()
.then(() => {
}).catch((err) => {
console.log(err)
})
}
如果不是ignoring
,我如何(或在哪里)向调用该函数的外部调用返回某种信号addItem
?例如,0
如果没有冲突(并且添加项目)则返回;1
如果有冲突返回(并且承诺悄悄地解决而不做任何事情)?谢谢
解决方案
可能是依赖于数据库的功能,但是例如,如果您使用 postgres 插入并且它不返回任何行,则意味着存在此问题中提到的冲突:
如何在 PostgreSQL 中使用 RETURNING 和 ON CONFLICT?
似乎也.returning('*')
可以与.onConflict(...).ignore()
https://runkit.com/embed/6p825ex0xt4b
knex('table').insert({a: 1, b:2}).onConflict('id').ignore().returning('*').toSQL().sql;
// knex 0.21.15 returns:
// insert into "table" ("a", "b") values (?, ?)
// on conflict ("id") do nothing returning *
推荐阅读
- javascript - JS 中的 CSS 可以正常工作,但在情感/样式中的相同样式根本不起作用
- vba - 有没有办法使用 VBA 以编程方式更改 Word 2010 中 CanvasShapes 的 Z 顺序位置?
- django - django.urls.exceptions.NoReverseMatch:“board_topics”的反向参数“(”,)”未找到
- flutter - 文本字段打开键盘颤动
- glfw - Mingw-w64 的 GLFW 库
- python - 如何在 Python 中访问 groupcollide() 返回的列表中的每个项目属性?
- firebase-authentication - Firebase 不接受从 Google Identity Platform 的 REST API 获取的 JWT:“自定义令牌格式不正确。”
- r - 如何通过R包中的字符串调用内部数据集
- python - Pandas 在另一列 B 中为 A 列中的选定值设置条目
- reactjs - React Native - 模态 - 动态最大高度