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 an attribute view, the block key (the primary key column holding block references) must exist before the view can be configured with rows. If GetBlockKeyValues returns nil or a structure without a Key, the view is malformed and configuration cannot continue.

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 8641553a1f)

Solutions

  1. Initialize the block (primary) key on the attribute view before configuring, e.g. via the standard key-setup code used by CreateAttributeViewDatabase
  2. In tests, use the same construction helper the production code uses so the primary key exists
  3. If loading an existing view, re-create or repair it so the block key column is present

Example fix

// before
attrView := av.NewAttributeView(avID)
err := model.ConfigureCreatedAttributeViewForTest(attrView, name, "Name", keys)
// after
attrView := av.NewAttributeView(avID)
attrView.KeyValues = append(attrView.KeyValues, &av.KeyValues{Key: &av.Key{ID: av.NewNodeID(), Name: "Name", Type: av.KeyTypeBlock}})
err := model.ConfigureCreatedAttributeViewForTest(attrView, name, "Name", keys)
Defensive patterns

Strategy: validation

Validate before calling

if attrView.GetBlockKeyValues() == nil || attrView.GetBlockKeyValues().Key == nil {
    return errors.New("attribute view fixture must define the block (primary) key")
}

Try / catch

if err := configureCreatedAttributeView(attrView, name, pk, keys); err != nil {
    return fmt.Errorf("attribute view missing primary key: %w", err)
}

Prevention

When it happens

Trigger: configureCreatedAttributeView receiving an attribute view whose block key was never initialized — e.g. a fixture built in tests without av.NewBlockKeyValues, or a view created through a path that skipped primary-key setup.

Common situations: Test fixtures that construct av.AttributeView manually and forget the block key; an upgraded/legacy attribute view JSON missing the primary key; a creation bug in the key-initialization code path.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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