geektutu/7days-golang · error

invalid sql type %s (%s)

Error message

invalid sql type %s (%s)

What it means

Identical to error 103 but in the day3-save-query stage: the sqlite3 dialect panics with 'invalid sql type' when Schema.Parse encounters a field type it cannot map to a SQLite column type. Only basic kinds and time.Time are supported; all other kinds fail fast.

Source

Thrown at gee-orm/day3-save-query/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. Use a supported Go type for the field or serialize to string/[]byte
  2. Register a custom dialect mapping if you control the dialect code
  3. Remove the field from the persisted struct or move it to a related table

Example fix

// before
type Order struct {
    Items []string
}
// after
type Order struct {
    Items string // JSON-encoded []string
}
Defensive patterns

Strategy: validation

Validate before calling

func checkSchemaTypes(v interface{}) error {
    t := reflect.TypeOf(v).Elem()
    for i := 0; i < t.NumField(); i++ {
        k := t.Field(i).Type.Kind()
        if k == reflect.Struct && t.Field(i).Type != reflect.TypeOf(time.Time{}) {
            return fmt.Errorf("field %s: unsupported %s", t.Field(i).Name, t.Field(i).Type)
        }
        if k == reflect.Map || k == reflect.Interface || k == reflect.Ptr {
            return fmt.Errorf("field %s: unsupported kind %s", t.Field(i).Name, k)
        }
    }
    return nil
}

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 model field: %s", s)
        } else { panic(r) }
    }
}()

Prevention

When it happens

Trigger: Calling geeorm.Open(db, &Model{...}) where Model has a field of unsupported kind (map, slice, embedded struct, custom kind) causing schema parse to call DataTypeOf with an unmappable type.

Common situations: Adding a new struct field for a feature and immediately opening a session against it; using pointer fields; storing collections directly in the row.

Related errors


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