siyuan-note/siyuan · error

attribute view has no primary key field

Error message

attribute view has no primary key field

What it means

After creating the attribute view, configureCreatedAttributeView fetches its block (primary) key values via GetBlockKeyValues and requires that the key exists. A nil key values or nil Key means the new database lacks its mandatory primary (block) key field, which SiYuan databases always have, so configuration aborts. This indicates the default primary key was not created during initialization.

Solutions

  1. Initialize the attribute view with a block key (av.NewKeyValues with the block key) before configuring
  2. Use the normal CreateAttributeViewDatabase path, which creates the primary key automatically, instead of hand-building the view
  3. Inspect the created keys and ensure the key whose type is block was not removed

Example fix

// before
attrView := &av.AttributeView{ID: avID}
err = configureCreatedAttributeView(attrView, name, pk, keys)
// after
blockKeyValues := &av.KeyValues{Key: &av.Key{ID: ast.NewNodeID(), Name: av.NodeAttrNameAvs, Type: av.KeyTypeBlock}}
attrView := av.NewAttributeView(avID)
attrView.KeyValues = append(attrView.KeyValues, blockKeyValues)
err = configureCreatedAttributeView(attrView, name, pk, keys)
Defensive patterns

Strategy: validation

Validate before calling

bkv := attrView.GetBlockKeyValues()
if bkv == nil || bkv.Key == nil { return errors.New("attribute view lacks primary key; re-create it") }

Type guard

func hasPrimaryKey(av *av.AttributeView) bool {
    kv := av.GetBlockKeyValues()
    return kv != nil && kv.Key != nil
}

Try / catch

if err := configureCreatedAttributeView(attrView, ...); err != nil {
    if strings.Contains(err.Error(), "no primary key field") { return recreateAttributeView(avID) }
    return err
}

Prevention

When it happens

Trigger: Calling configureCreatedAttributeView on an attribute view that was not initialized with a block key (e.g. hand-constructed av.AttributeView without keyValues); a failure in the creation path that silently skipped adding the block key.

Common situations: Tests or plugins building av.AttributeView structs manually and passing them to the helper; corruption or partial writes during database creation; filtering out the block key when constructing keys.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/678b954936563bed. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/attribute_view_create.go:134

	if bt := treenode.GetBlockTree(blockID); nil != bt {
		util.PushReloadProtyle(bt.RootID)
	}
	ReloadAttrView(avID)
	ret = &AttributeViewCreateResult{BlockID: blockID, AvID: avID, ViewID: view.ID, AttributeView: attrView}
	return
}

func configureCreatedAttributeView(attrView *av.AttributeView, name, primaryKeyName string, preparedKeys []*av.Key) (err error) {
	if nil == attrView {
		return errors.New("attribute view is required")
	}
	currentView, err := attrView.GetFirstView()
	if nil != err {
		return
	}
	blockKeyValues := attrView.GetBlockKeyValues()
	if nil == blockKeyValues || nil == blockKeyValues.Key {
		return errors.New("attribute view has no primary key field")
	}

	if name = strings.TrimSpace(name); "" != name {
		name = strings.ReplaceAll(name, "\n", " ")
		if 512 < utf8.RuneCountInString(name) {
			name = string([]rune(name)[:512])
		}
		attrView.Name = name
	}
	if primaryKeyName = strings.TrimSpace(primaryKeyName); "" != primaryKeyName {
		blockKeyValues.Key.Name = primaryKeyName
	}

	retainedKeyValues := []*av.KeyValues{blockKeyValues}
	retainedKeyIDs := map[string]bool{blockKeyValues.Key.ID: true}
	var kanbanGroupKey *av.Key
	if av.LayoutTypeKanban == currentView.LayoutType {
		for _, key := range preparedKeys {

View on GitHub (pinned to 9f775e8a12)