siyuan-note/siyuan · error

cannot change field type to primary key

Error message

cannot change field type to primary key

What it means

Companion to the primary-key protection: the kernel forbids changing any existing non-block column INTO the block (primary key) type. Only one primary key exists per attribute view, and it is created with the view; promoting another column to block type is rejected to keep row identity unambiguous.

Solutions

  1. Do not offer/select 'block' as a change target for existing keys; it is only set at view creation
  2. Remove operation.ID/type-change operations that set Typ to block
  3. Create a new view or use the existing block key instead of promoting a column
  4. Adjust the plugin to disable the block option in the type menu

Example fix

// before
op.Typ = "block" // trying to promote column to primary key
// after
op.Typ = "text" // use an existing key type; the block key already exists in the view
Defensive patterns

Strategy: validation

Validate before calling

if (op.Typ === "block") throw new Error("cannot promote an existing key to block; the primary key already exists");

Type guard

const isPromotionToBlock = (targetType) => targetType === "block";

Try / catch

try { await updateColType(op); } catch (e) { if (String(e).includes("cannot change field type to primary key")) { /* remove the type change or target a different type */ } else { throw e; } }

Prevention

When it happens

Trigger: Calling the update-column type-change API with operation.Typ = 'block' targeting a key whose current type is not block.

Common situations: Plugin UI that offers 'block' in a generic type dropdown; scripts converting CSV-like data where one column is intended as the row identifier; bulk type normalization code.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/attribute_view.go:7710

	attrView, err := av.ParseAttributeView(operation.AvID)
	if err != nil {
		return
	}

	colType := av.KeyType(operation.Typ)
	changeType := false
	switch colType {
	case av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail,
		av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated, av.KeyTypeCheckbox,
		av.KeyTypeRelation, av.KeyTypeRollup, av.KeyTypeLineNumber:
		for _, keyValues := range attrView.KeyValues {
			if keyValues.Key.ID == operation.ID {
				isPrimaryKey := av.KeyTypeBlock == keyValues.Key.Type
				if isPrimaryKey != (av.KeyTypeBlock == colType) {
					if isPrimaryKey {
						err = errors.New("cannot change type of primary key field")
					} else {
						err = errors.New("cannot change field type to primary key")
					}
					return
				}

				keyValues.Key.Name = strings.TrimSpace(operation.Name)

				changeType = keyValues.Key.Type != colType
				keyValues.Key.Type = colType

				for _, value := range keyValues.Values {
					value.Type = colType
				}

				break
			}
		}
	}

View on GitHub (pinned to 9f775e8a12)