我想知道是否可以通过简单地更新关系一侧的模型来删除两个模型之间的关系。添加新关系效果很好,但删除似乎不是一种选择,但也许我只是错过了一些东西。insertOrUpdate()
不“删除”嵌套关系似乎是合乎逻辑的,但也许有另一个函数或属性如此设置以获得所需的行为。不幸的是,搜索文档没有成功。
在我的例子中,我belongsToMany
在模型“进程”和模型“依赖”之间配置了一个 (m:n) 关系。中间的 mn 模型是“MapProcessDependency”
模型
// Dependency.js
export default class Dependency extends Model {
static entity = 'dependencies'
static fields () {
return {
id: this.attr(null),
name: this.string('')
}
}
}
// Process.js
export default class Process extends Model {
static entity = 'processes'
static fields () {
return {
id: this.attr(null),
name: this.string(''),
dependencies: this.belongsToMany(
Dependency, MapProcessDependency, 'processId', 'dependencyId'
)
}
}
}
// MapProcessDependency
export default class MapProcessDependency extends Model {
static entity = 'mapProcessesDependencies'
static primaryKey = ['processId', 'dependencyId']
static fields () {
return {
processId: this.attr(null),
dependencyId: this.attr(null)
}
}
}
Vuex-ORM 数据库
dependencies: [
{
"id": 1,
"name": "Dep1",
"$id": "1"
},
{
"id": 2,
"name": "Dep2",
"$id": "2"
}
],
processes: [
{
"id": 99,
"name": "MyProc",
"dependencies": [ /* managed via vuex-orm in mapProcessesDependencies */],
"$id": "99"
}
],
mapProcessesDependencies: [
"[99,1]": {
"processId": 99,
"dependencyId": 1,
"$id": "[99,1]"
},
"[99,2]": {
"processId": 99,
"dependencyId": 2,
"$id": "[99,2]"
}
]
我想要达到的目标
// ...by calling this:
Process.insertOrUpdate({ where: 99, data: {
"id": 99,
"name": "MyProc",
"dependencies": [ 1 ],
} })
...是以下结果,无需手动调用MapProcessDependency.delete([99,2])
:
// [...]
mapProcessesDependencies: [
"[99,1]": {
"processId": 99,
"dependencyId": 1,
"$id": "[99,1]"
}
// relationship [99,2] removed
]