vitessio/vitess · error

expected a CREATE VIEW statement

Error message

expected a CREATE VIEW statement

What it means

ErrExpectedCreateView is returned by DiffCreateViewsQueries when a view entity's statement is not a CREATE VIEW statement. View diffing only operates on CREATE VIEW definitions, so any other statement type in the entity list triggers this error.

Source

Thrown at go/vt/schemadiff/errors.go:34

package schemadiff

import (
	"errors"
	"fmt"
	"strings"

	"vitess.io/vitess/go/sqlescape"
	"vitess.io/vitess/go/vt/sqlparser"
)

var (
	ErrEntityTypeMismatch             = errors.New("mismatched entity type")
	ErrStrictIndexOrderingUnsupported = errors.New("strict index ordering is unsupported")
	ErrUnexpectedDiffAction           = errors.New("unexpected diff action")
	ErrUnexpectedTableSpec            = errors.New("unexpected table spec")
	ErrExpectedCreateTable            = errors.New("expected a CREATE TABLE statement")
	ErrExpectedCreateView             = errors.New("expected a CREATE VIEW statement")
)

type ImpossibleApplyDiffOrderError struct {
	UnorderedDiffs   []EntityDiff
	ConflictingDiffs []EntityDiff
}

func (e *ImpossibleApplyDiffOrderError) Error() string {
	var b strings.Builder
	conflictingStatements := e.ConflictingStatements()
	fmt.Fprintf(&b, "no valid applicable order for diffs. %d diffs found conflicting:", len(conflictingStatements))
	for _, s := range conflictingStatements {
		b.WriteString("\n")
		b.WriteString(s)
	}
	return b.String()
}

View on GitHub (pinned to 01a25a7d17)

Solutions

  1. Filter entity lists so only CREATE VIEW entities go to DiffCreateViewsQueries
  2. Use DiffCreateTablesQueries for table entities
  3. Recreate view entities with NewCreateViewEntityFromSQL from the original CREATE VIEW text
Defensive patterns

Strategy: type-guard

Validate before calling

stmt, err := sqlparser.Parse(ddl)
ct, ok := stmt.(*sqlparser.CreateTable)
if ok && ct.ViewSpec != nil { /* it is a view; use view APIs */ }

Type guard

func isCreateView(e schemadiff.Entity) bool {
	_, ok := e.(*schemadiff.CreateViewEntity)
	return ok
}

Try / catch

if err != nil {
	if errors.Is(err, schemadiff.ErrExpectedCreateView) {
		// route to table diffing instead
	}
}

Prevention

When it happens

Trigger: Calling DiffCreateViewsQueries with entities built from table DDL or ALTER statements instead of CREATE VIEW statements.

Common situations: Schema-loading code that puts all DDL (tables + views) through the view diff path; renamed statements (e.g. CREATE OR REPLACE VIEW variants) that no longer parse as plain CREATE VIEW in the code path.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/ad93922bf4d69b44. Report an issue: GitHub.