Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Add Relation and Rollup column to database table view https://github.com/siyuan-note/siyuan/issues/9888

Daniel46c82acf6cd0
diff --git a/kernel/av/av.go b/kernel/av/av.go
index 0767c850c5..7f793f2729 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -89,12 +89,10 @@ type Key struct {
Template string `json:"template"` // 模板内容
// 关联列
- RelationAvID string `json:"relationAvID"` // 关联的属性视图 ID
- IsBiRelation bool `json:"isBiRelation"` // 是否双向关联
- BackRelationKeyID string `json:"backRelationKeyID"` // 双向关联时回链关联列的 ID
+ Relation *Relation `json:"relation,omitempty"` // 关联信息
// 汇总列
- RollupKeyID string `json:"rollupKeyID"` // 汇总列 ID
+ Rollup *Rollup `json:"rollup,omitempty"` // 汇总信息
}
func NewKey(id, name, icon string, keyType KeyType) *Key {
@@ -106,6 +104,16 @@ func NewKey(id, name, icon string, keyType KeyType) *Key {
}
}
+type Rollup struct {
+ KeyID string `json:"keyID"` // 汇总列 ID
+}
+
+type Relation struct {
+ AvID string `json:"avID"` // 关联的属性视图 ID
+ IsTwoWay bool `json:"isTwoWay"` // 是否双向关联
+ BackKeyID string `json:"backKeyID"` // 双向关联时回链关联列的 ID
+}
+
type KeySelectOption struct {
Name string `json:"name"`
Color string `json:"color"`
diff --git a/kernel/av/table.go b/kernel/av/table.go
index 50f47e4d6a..71dd16cfd8 100644
--- a/kernel/av/table.go
+++ b/kernel/av/table.go
@@ -651,9 +651,11 @@ type TableColumn struct {
// 以下是某些列类型的特有属性
- Options []*KeySelectOption `json:"options,omitempty"` // 选项列表
- NumberFormat NumberFormat `json:"numberFormat"` // 列数字格式化
- Template string `json:"template"` // 模板内容
+ Options []*KeySelectOption `json:"options,omitempty"` // 选项列表
+ NumberFormat NumberFormat `json:"numberFormat"` // 列数字格式化
+ Template string `json:"template"` // 模板内容
+ Relation *Relation `json:"relation,omitempty"` // 关联列
+ Rollup *Rollup `json:"rollup,omitempty"` // 汇总列
}
type TableCell struct {
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index d9f05c255b..648fc90668 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -553,6 +553,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
Options: key.Options,
NumberFormat: key.NumberFormat,
Template: key.Template,
+ Relation: key.Relation,
+ Rollup: key.Rollup,
Wrap: col.Wrap,
Hidden: col.Hidden,
Width: col.Width,
@@ -728,32 +730,66 @@ func (tx *Transaction) doUpdateAttrViewColRelation(operation *Operation) (ret *T
}
func updateAttributeViewColRelation(operation *Operation) (err error) {
- err = updateAttributeViewColRelation0(operation.AvID, operation.KeyID, operation.ID, operation.IsBiRelation, operation.BackRelationKeyID, operation.Name)
+ // operation.AvID 源 avID
+ // operation.ID 目标 avID
+ // operation.KeyID 源 av 关联列 ID
+ // operation.IsTwoWay 是否双向关联
+ // operation.BackRelationKeyID 双向关联的目标关联列 ID
+ // operation.Name 双向关联的目标关联列名称
+
+ srcAv, err := av.ParseAttributeView(operation.AvID)
if nil != err {
return
}
- if operation.IsBiRelation {
- err = updateAttributeViewColRelation0(operation.ID, operation.BackRelationKeyID, operation.AvID, operation.IsBiRelation, operation.KeyID, operation.Name)
- }
- return
-}
-
-func updateAttributeViewColRelation0(avID, relKeyID, destAvID string, isBiRel bool, backRelKeyID, backRelKeyName string) (err error) {
- attrView, err := av.ParseAttributeView(avID)
+ destAv, err := av.ParseAttributeView(operation.ID)
if nil != err {
return
}
- for _, keyValues := range attrView.KeyValues {
- if keyValues.Key.ID == relKeyID {
- keyValues.Key.RelationAvID = destAvID
- keyValues.Key.IsBiRelation = isBiRel
- keyValues.Key.BackRelationKeyID = backRelKeyID
- err = av.SaveAttributeView(attrView)
- return
+ isSameAv := srcAv.ID == destAv.ID
+
+ for _, keyValues := range srcAv.KeyValues {
+ if keyValues.Key.ID == operation.KeyID {
+ keyValues.Key.Relation = &av.Relation{
+ AvID: operation.ID,
+ IsTwoWay: operation.IsTwoWay,
+ BackKeyID: operation.BackRelationKeyID,
+ }
+ break
}
}
+
+ destAdded := false
+ for _, keyValues := range destAv.KeyValues {
+ if keyValues.Key.ID == operation.BackRelationKeyID {
+ keyValues.Key.Relation = &av.Relation{
+ AvID: operation.AvID,
+ IsTwoWay: operation.IsTwoWay,
+ BackKeyID: operation.KeyID,
+ }
+ destAdded = true
+ break
+ }
+ }
+ if !destAdded {
+ destAv.KeyValues = append(destAv.KeyValues, &av.KeyValues{
+ Key: &av.Key{
+ ID: operation.BackRelationKeyID,
+ Name: operation.Name,
+ Type: av.KeyTypeRelation,
+ Relation: &av.Relation{AvID: operation.AvID, IsTwoWay: operation.IsTwoWay, BackKeyID: operation.KeyID},
+ },
+ })
+ }
+
+ err = av.SaveAttributeView(srcAv)
+ if nil != err {
+ return
+ }
+ if !isSameAv {
+ err = av.SaveAttributeView(destAv)
+ }
return
}
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 3647eb77d9..35153b8355 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1191,7 +1191,7 @@ type Operation struct {
Format string `json:"format"` // 属性视图列格式化
KeyID string `json:"keyID"` // 属性视列 ID
RowID string `json:"rowID"` // 属性视图行 ID
- IsBiRelation bool `json:"isBiRelation"` // 属性视图关联列是否是双向关系
+ IsTwoWay bool `json:"isTwoWay"` // 属性视图关联列是否是双向关系
BackRelationKeyID string `json:"backRelationKeyID"` // 属性视图关联列回链关联列的 ID
}
diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go
index 1e07f7d865..5bca7a517f 100644
--- a/kernel/treenode/node.go
+++ b/kernel/treenode/node.go
@@ -614,6 +614,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
Options: key.Options,
NumberFormat: key.NumberFormat,
Template: key.Template,
+ Relation: key.Relation,
+ Rollup: key.Rollup,
Wrap: col.Wrap,
Hidden: col.Hidden,
Width: col.Width,