Skip to content

Commit

Permalink
🎨 Improve interaction when deleting two-way relation field in a datab…
Browse files Browse the repository at this point in the history
…ase #11252
  • Loading branch information
88250 committed Oct 17, 2024
1 parent ca330e2 commit 88e321b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
6 changes: 5 additions & 1 deletion kernel/api/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,12 @@ func removeAttributeViewKey(c *gin.Context) {

avID := arg["avID"].(string)
keyID := arg["keyID"].(string)
removeRelationDest := false
if nil != arg["removeRelationDest"] {
removeRelationDest = arg["removeRelationDest"].(bool)
}

err := model.RemoveAttributeViewKey(avID, keyID)
err := model.RemoveAttributeViewKey(avID, keyID, removeRelationDest)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
Expand Down
68 changes: 34 additions & 34 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2772,14 +2772,14 @@ func updateAttributeViewColumn(operation *Operation) (err error) {
}

func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr) {
err := RemoveAttributeViewKey(operation.AvID, operation.ID)
err := RemoveAttributeViewKey(operation.AvID, operation.ID, operation.RemoveDest)
if err != nil {
return &TxErr{code: TxErrWriteAttributeView, id: operation.AvID, msg: err.Error()}
}
return
}

func RemoveAttributeViewKey(avID, keyID string) (err error) {
func RemoveAttributeViewKey(avID, keyID string, removeRelationDest bool) (err error) {
attrView, err := av.ParseAttributeView(avID)
if err != nil {
return
Expand All @@ -2796,47 +2796,47 @@ func RemoveAttributeViewKey(avID, keyID string) (err error) {

if nil != removedKey && av.KeyTypeRelation == removedKey.Type && nil != removedKey.Relation {
if removedKey.Relation.IsTwoWay {
// 删除双向关联的目标列

var destAv *av.AttributeView
if avID == removedKey.Relation.AvID {
destAv = attrView
} else {
destAv, _ = av.ParseAttributeView(removedKey.Relation.AvID)
}
if removeRelationDest { // 删除双向关联的目标列
var destAv *av.AttributeView
if avID == removedKey.Relation.AvID {
destAv = attrView
} else {
destAv, _ = av.ParseAttributeView(removedKey.Relation.AvID)
}

if nil != destAv {
destAvRelSrcAv := false
for i, keyValues := range destAv.KeyValues {
if keyValues.Key.ID == removedKey.Relation.BackKeyID {
destAv.KeyValues = append(destAv.KeyValues[:i], destAv.KeyValues[i+1:]...)
continue
}
if nil != destAv {
destAvRelSrcAv := false
for i, keyValues := range destAv.KeyValues {
if keyValues.Key.ID == removedKey.Relation.BackKeyID {
destAv.KeyValues = append(destAv.KeyValues[:i], destAv.KeyValues[i+1:]...)
continue
}

if av.KeyTypeRelation == keyValues.Key.Type && keyValues.Key.Relation.AvID == attrView.ID {
destAvRelSrcAv = true
if av.KeyTypeRelation == keyValues.Key.Type && keyValues.Key.Relation.AvID == attrView.ID {
destAvRelSrcAv = true
}
}
}

for _, view := range destAv.Views {
switch view.LayoutType {
case av.LayoutTypeTable:
for i, column := range view.Table.Columns {
if column.ID == removedKey.Relation.BackKeyID {
view.Table.Columns = append(view.Table.Columns[:i], view.Table.Columns[i+1:]...)
break
for _, view := range destAv.Views {
switch view.LayoutType {
case av.LayoutTypeTable:
for i, column := range view.Table.Columns {
if column.ID == removedKey.Relation.BackKeyID {
view.Table.Columns = append(view.Table.Columns[:i], view.Table.Columns[i+1:]...)
break
}
}
}
}
}

if destAv != attrView {
av.SaveAttributeView(destAv)
ReloadAttrView(destAv.ID)
}
if destAv != attrView {
av.SaveAttributeView(destAv)
ReloadAttrView(destAv.ID)
}

if !destAvRelSrcAv {
av.RemoveAvRel(destAv.ID, attrView.ID)
if !destAvRelSrcAv {
av.RemoveAvRel(destAv.ID, attrView.ID)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions kernel/model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,7 @@ type Operation struct {
RowID string `json:"rowID"` // 属性视图行 ID
IsTwoWay bool `json:"isTwoWay"` // 属性视图关联列是否是双向关系
BackRelationKeyID string `json:"backRelationKeyID"` // 属性视图关联列回链关联列的 ID
RemoveDest bool `json:"removeDest"` // 属性视图删除关联目标
}

type Transaction struct {
Expand Down

0 comments on commit 88e321b

Please sign in to comment.