postgresql - 在 Postgres 中返回不受影响的行
问题描述
我有一个 Postgres 数据库并使用ObjectionJS作为我的 ORM。
我有以下 ObjectionJS 代码来修补表的两列user_system
。
public static async DeleteSystem(systemId: number, context: Context): Promise<UserSystem> {
const system = await UserSystem.query().context(context).patch({
state: PointInTimeState.inactive,
receiveNotifications: false
}).where({
system_id: systemId
}).throwIfNotFound().debug()
//("as any" for now because the system variable is a number at this point (i.e NOT of type Promise<UserSystem> as in the signature)
return system as any
}
问题
- 有没有办法可以返回所有不受此补丁影响的行?
- 如果是这样,如何不必向后端编写两个单独的查询(更新然后重新查询新数据)?
解决方案
据我所知,编写 CTE 将两个查询组合为单个查询是唯一的方法。
有异议/ knex 他们可以用https://knexjs.org/#Builder-with
因此,在查询中,您执行.patch(...).where(...).returning('id')
并在主查询中选择表中的所有行,这些行不在第一个查询返回的 id 集中。
像这样的东西(不确定这是否像这样有异议):
UserSystem.query().context(context).with('patchedIds',
UserSystem.query().patch({
state: PointInTimeState.inactive,
receiveNotifications: false
}).where({
system_id: systemId
}).returning('id')
).whereNotIn('id', builder => {
builder.from('patchedId').select('id')
})
推荐阅读
- reactjs - React - 传递多个选项参数/值 onChange
- javascript - Jquery 和 JS 的新手:尝试让相应的价格显示在总数中
- javascript - 反应:在渲染时,给每个 div 一个唯一的 className
- python - 推送我的 django 项目时,Heroku 无法在我的“requirements.txt”中安装依赖项
- python - Pandas:当组中的值落在某个范围内时,将组保留在数据中
- python - 属性错误:“球”对象没有属性“x”
- sql - 如何将0添加到int
- python - Keras LSTM 网络预测更新
- python - 使用多列值的折线图
- python - 尝试/除了显示错误,但我不希望它