Browse Source

fix: handle foreign key constraint violation

Signed-off-by: Wing-Kam Wong <wingkwong.code@gmail.com>
pull/1333/head
Wing-Kam Wong 3 years ago
parent
commit
a74026f5b4
  1. 5
      packages/nc-gui/components/project/spreadsheet/components/virtualCell/components/listItems.vue
  2. 20
      packages/nc-gui/components/project/spreadsheet/components/virtualCell/hasManyCell.vue

5
packages/nc-gui/components/project/spreadsheet/components/virtualCell/components/listItems.vue

@ -150,7 +150,10 @@ export default {
if (!this.api) { if (!this.api) {
return return
} }
const isByPass = this.queryParams.isByPass || false
if (isByPass) {
return
}
let where = this.queryParams.where || '' let where = this.queryParams.where || ''
if (this.query) { if (this.query) {
where += (where ? '~and' : '') + `(${this.primaryCol},like,%${this.query}%)` where += (where ? '~and' : '') + `(${this.primaryCol},like,%${this.query}%)`

20
packages/nc-gui/components/project/spreadsheet/components/virtualCell/hasManyCell.vue

@ -54,7 +54,16 @@
:parent-meta="meta" :parent-meta="meta"
:query-params="{ :query-params="{
...childQueryParams, ...childQueryParams,
where: isNew ? null :`~not(${childForeignKey},eq,${parentId})~or(${childForeignKey},is,null)`, // return empty set if childForeignKeyVal is empty
// to avoid foreign key constraint violation in real relation
isByPass: !isVirtualRelation && childForeignKeyVal === '',
where:
// show all for new record
isNew ? null :
// filter out those selected items
`~not(${childForeignKey},eq,${parentId})` +
// allow the child with empty key
'~or(' + childForeignKey + ',is,null)'
}" }"
:is-public="isPublic" :is-public="isPublic"
:password="password" :password="password"
@ -221,6 +230,12 @@ export default {
childForeignKey() { childForeignKey() {
return this.childMeta && (this.childMeta.columns.find(c => c.cn === this.hm.cn) || {})._cn return this.childMeta && (this.childMeta.columns.find(c => c.cn === this.hm.cn) || {})._cn
}, },
childForeignKeyVal() {
return this.meta && this.meta.columns ? this.meta.columns.filter(c => c._cn === this.childForeignKey).map(c => this.row[c._cn] || '').join('___') : ''
},
isVirtualRelation() {
return (this.childMeta && (!!this.childMeta.columns.find(c => c.cn === this.hm.cn && this.hm.type === 'virtual'))) || false
},
disabledChildColumns() { disabledChildColumns() {
return { [this.childForeignKey]: true } return { [this.childForeignKey]: true }
}, },
@ -252,7 +267,7 @@ export default {
}, },
parentId() { parentId() {
return (this.meta && this.meta.columns && return (this.meta && this.meta.columns &&
(this.meta.columns.filter(c => c._cn === this.childForeignKey).map(c => this.row[c._cn]).join('___') || (this.meta.columns.filter(c => c._cn === this.childForeignKey).map(c => this.row[c._cn] || '').join('___') ||
this.meta.columns.filter(c => c.pk).map(c => this.row[c._cn]).join('___'))) || '' this.meta.columns.filter(c => c.pk).map(c => this.row[c._cn]).join('___'))) || ''
} }
}, },
@ -360,7 +375,6 @@ export default {
const id = this.childMeta.columns.filter(c => c.pk).map(c => child[c._cn]).join('___') const id = this.childMeta.columns.filter(c => c.pk).map(c => child[c._cn]).join('___')
const _cn = this.childForeignKey const _cn = this.childForeignKey
this.newRecordModal = false this.newRecordModal = false
await this.childApi.update(id, { await this.childApi.update(id, {
[_cn]: parseIfInteger(this.parentId) [_cn]: parseIfInteger(this.parentId)
}, { }, {

Loading…
Cancel
Save