siyuan-note/siyuan · error

database field name and type are required

Error message

database field name and type are required

What it means

Each key spec passed to CreateAttributeViewDatabase must have a non-empty Name and Type; a nil spec, an all-whitespace name, or an empty type triggers this error. These specs define the database fields created in order, and both name and type are mandatory to build an av.Key. The check is fail-fast before any key is created.

Solutions

  1. Ensure every key spec has a non-blank Name and a valid Type string before calling
  2. Filter out nil specs from the slice: keys = slices.DeleteFunc(keys, func(s *AttributeViewCreateKey) bool { return s == nil })
  3. Trim user input and reject empty names in the client before invoking the API

Example fix

// before
specs := []*model.AttributeViewCreateKey{{Name: " ", Type: "text"}, nil}
model.CreateAttributeViewDatabase(parentID, "", "", name, pk, layout, specs)
// after
specs = slices.DeleteFunc(specs, func(s *model.AttributeViewCreateKey) bool {
    return s == nil || strings.TrimSpace(s.Name) == "" || strings.TrimSpace(s.Type) == ""
})
model.CreateAttributeViewDatabase(parentID, "", "", name, pk, layout, specs)
Defensive patterns

Strategy: validation

Validate before calling

for i, s := range specs {
    if s == nil || strings.TrimSpace(s.Name) == "" || strings.TrimSpace(s.Type) == "" {
        return fmt.Errorf("spec %d: name and type are required", i)
    }
}

Try / catch

if err != nil && strings.Contains(err.Error(), "field name and type are required") {
    return sanitizeSpecsAndRetry(specs)
}

Prevention

When it happens

Trigger: Calling CreateAttributeViewDatabase with keySpecs containing a nil entry, or an entry whose Name or Type is "" or whitespace (e.g. " "); the databaseCreate API handler forwarding such values from the request body.

Common situations: Dynamically built field lists where empty user input is not filtered; JSON requests omitting the type field; whitespace-only names pasted from a template.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/attribute_view_create.go:74

	}
	if "" == previousID && "" == nextID {
		if err = treenode.CheckContainerParent(parentID); nil != err {
			return nil, err
		}
	}
	if "" == layout {
		layout = av.LayoutTypeTable
	}
	switch layout {
	case av.LayoutTypeTable, av.LayoutTypeGallery, av.LayoutTypeKanban:
	default:
		return nil, av.ErrWrongLayoutType
	}

	preparedKeys := make([]*av.Key, 0, len(keySpecs))
	for _, spec := range keySpecs {
		if nil == spec || "" == strings.TrimSpace(spec.Name) || "" == strings.TrimSpace(spec.Type) {
			return nil, errors.New("database field name and type are required")
		}
		key, keyErr := newAttributeViewKey(ast.NewNodeID(), strings.TrimSpace(spec.Name), strings.TrimSpace(spec.Type), spec.Icon,
			av.DateDisplayFormatFull)
		if nil != keyErr {
			return nil, keyErr
		}
		preparedKeys = append(preparedKeys, key)
	}

	blockID, avID := ast.NewNodeID(), ast.NewNodeID()
	data := fmt.Sprintf(`<div class="av" data-node-id="%s" data-av-id="%s" data-type="NodeAttributeView" data-av-type="%s"></div>`,
		blockID, avID, layout)
	operation := &Operation{
		Action:     "insert",
		Data:       data,
		ParentID:   parentID,
		PreviousID: previousID,
		NextID:     nextID,

View on GitHub (pinned to 9f775e8a12)