Browse Source

:bug: Abnormal sorting of database relation fields https://github.com/siyuan-note/siyuan/issues/10835

Daniel 1 year ago
parent
commit
eae61fd4a7
4 changed files with 16 additions and 14 deletions
  1. 1 1
      kernel/av/av.go
  2. 9 3
      kernel/av/value.go
  3. 5 9
      kernel/model/attribute_view.go
  4. 1 1
      kernel/treenode/node.go

+ 1 - 1
kernel/av/av.go

@@ -420,7 +420,7 @@ func SaveAttributeView(av *AttributeView) (err error) {
 			}
 			}
 
 
 			if 0 == v.UpdatedAt {
 			if 0 == v.UpdatedAt {
-				v.UpdatedAt = v.CreatedAt
+				v.UpdatedAt = v.CreatedAt + 1000
 			}
 			}
 		}
 		}
 	}
 	}

+ 9 - 3
kernel/av/value.go

@@ -57,6 +57,13 @@ type Value struct {
 	Rollup   *ValueRollup   `json:"rollup,omitempty"`
 	Rollup   *ValueRollup   `json:"rollup,omitempty"`
 }
 }
 
 
+func (value *Value) SetUpdatedAt(mills int64) {
+	value.UpdatedAt = mills
+	if value.CreatedAt == value.UpdatedAt {
+		value.UpdatedAt += 1000 // 防止更新时间和创建时间一样
+	}
+}
+
 func (value *Value) String() string {
 func (value *Value) String() string {
 	if nil == value {
 	if nil == value {
 		return ""
 		return ""
@@ -198,9 +205,8 @@ func (value *Value) IsEdited() bool {
 		return true
 		return true
 	}
 	}
 
 
-	if value.IsEmpty() {
-		// 空数据认为是未编辑过的
-		return false
+	if !value.IsEmpty() {
+		return true
 	}
 	}
 	return value.CreatedAt != value.UpdatedAt
 	return value.CreatedAt != value.UpdatedAt
 }
 }

+ 5 - 9
kernel/model/attribute_view.go

@@ -687,7 +687,7 @@ func renderAttributeView(attrView *av.AttributeView, viewID, query string, page,
 			}
 			}
 
 
 			if 0 == v.UpdatedAt {
 			if 0 == v.UpdatedAt {
-				v.UpdatedAt = v.CreatedAt
+				v.UpdatedAt = v.CreatedAt + 1000
 			}
 			}
 		}
 		}
 	}
 	}
@@ -2913,18 +2913,12 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
 
 
 	if nil != blockVal {
 	if nil != blockVal {
 		blockVal.Block.Updated = now
 		blockVal.Block.Updated = now
-		blockVal.UpdatedAt = now
-		if val.CreatedAt == val.UpdatedAt {
-			val.UpdatedAt += 1000 // 防止更新时间和创建时间一样
-		}
+		blockVal.SetUpdatedAt(now)
 		if isUpdatingBlockKey {
 		if isUpdatingBlockKey {
 			blockVal.IsDetached = val.IsDetached
 			blockVal.IsDetached = val.IsDetached
 		}
 		}
 	}
 	}
-	val.UpdatedAt = now
-	if val.CreatedAt == val.UpdatedAt {
-		val.UpdatedAt += 1000 // 防止更新时间和创建时间一样
-	}
+	val.SetUpdatedAt(now)
 
 
 	key, _ := attrView.GetKey(val.KeyID)
 	key, _ := attrView.GetKey(val.KeyID)
 	if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation {
 	if nil != key && av.KeyTypeRelation == key.Type && nil != key.Relation {
@@ -2956,6 +2950,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
 
 
 							destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, rowID)
 							destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, rowID)
 							destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
 							destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
+							destVal.SetUpdatedAt(now)
 							break
 							break
 						}
 						}
 					}
 					}
@@ -2974,6 +2969,7 @@ func UpdateAttributeViewCell(tx *Transaction, avID, keyID, rowID, cellID string,
 							for _, value := range keyValues.Values {
 							for _, value := range keyValues.Values {
 								if value.BlockID == blockID {
 								if value.BlockID == blockID {
 									value.Relation.BlockIDs = gulu.Str.RemoveElem(value.Relation.BlockIDs, rowID)
 									value.Relation.BlockIDs = gulu.Str.RemoveElem(value.Relation.BlockIDs, rowID)
+									value.SetUpdatedAt(now)
 									break
 									break
 								}
 								}
 							}
 							}

+ 1 - 1
kernel/treenode/node.go

@@ -995,7 +995,7 @@ func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ av.KeyType
 		ret.CreatedAt = time.Now().UnixMilli()
 		ret.CreatedAt = time.Now().UnixMilli()
 	}
 	}
 	if 0 == ret.UpdatedAt {
 	if 0 == ret.UpdatedAt {
-		ret.UpdatedAt = ret.CreatedAt
+		ret.UpdatedAt = ret.CreatedAt + 1000
 	}
 	}
 
 
 	switch typ {
 	switch typ {