geektutu/7days-golang · error

invalid sql type %s (%s)

Error message

invalid sql type %s (%s)

What it means

Same 'invalid sql type' panic in the day5-hooks stage of gee-orm: DataTypeOf for the sqlite3 dialect panics on any reflect.Type outside its supported switch (bool/int kinds, float, string, []byte, time.Time struct). Triggered while parsing the schema for Open or query building.

Source

Thrown at gee-orm/day5-hooks/dialect/sqlite3.go:38

	case reflect.Bool:
		return "bool"
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
		return "integer"
	case reflect.Int64, reflect.Uint64:
		return "bigint"
	case reflect.Float32, reflect.Float64:
		return "real"
	case reflect.String:
		return "text"
	case reflect.Array, reflect.Slice:
		return "blob"
	case reflect.Struct:
		if _, ok := typ.Interface().(time.Time); ok {
			return "datetime"
		}
	}
	panic(fmt.Sprintf("invalid sql type %s (%s)", typ.Type().Name(), typ.Kind()))
}

// TableExistSQL returns SQL that judge whether the table exists in database
func (s *sqlite3) TableExistSQL(tableName string) (string, []interface{}) {
	args := []interface{}{tableName}
	return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Change the field type to one of the supported kinds
  2. Pre-serialize complex values into string/[]byte columns
  3. Add explicit dialect support for the kind

Example fix

// before
type Session struct {
    Data map[string]interface{}
}
// after
type Session struct {
    Data string // JSON encoded
}
Defensive patterns

Strategy: validation

Validate before calling

for _, f := range reflect.VisibleFields(reflect.TypeOf(Model{})) {
    if !isSQLiteMappable(f.Type) {
        log.Fatalf("field %s of type %s is not sqlite-mappable", f.Name, f.Type)
    }
}

Type guard

func isSQLiteMappable(t reflect.Type) bool {
    if t == reflect.TypeOf(time.Time{}) { return true }
    switch t.Kind() {
    case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
        reflect.Float32, reflect.Float64, reflect.String, reflect.Slice:
        return true
    }
    return false
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        if s, ok := r.(string); ok && strings.HasPrefix(s, "invalid sql type") {
            err = fmt.Errorf("unsupported schema field: %s", s)
        } else { panic(r) }
    }
}()

Prevention

When it happens

Trigger: Opening a session against a model with an unmappable field type (map, slice of structs, custom struct kind, interface field).

Common situations: Persisting struct fields added by hooks/feature work; modeling JSON documents inline; interface-typed fields.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/f424d03437ec57ff. Report an issue: GitHub.