micro/go-micro · error

failed to prepare delete statement

Error message

failed to prepare delete statement

What it means

initDB prepares the DELETE statement used by sqlStore.Delete; failure aborts store configuration. As with the other prepare steps, this almost always means a dead connection or invalid interpolated SQL identifiers.

Source

Thrown at store/mysql/mysql.go:176

		return errors.Wrap(err, "Couldn't create table")
	}

	// prepare statements
	var prepareErr error

	s.readPrepare, prepareErr = s.db.Prepare(fmt.Sprintf("SELECT `key`, value, expiry FROM %s.%s WHERE `key` = ?;", s.database, s.table))
	if prepareErr != nil {
		return errors.Wrap(prepareErr, "failed to prepare read statement")
	}

	s.writePrepare, prepareErr = s.db.Prepare(fmt.Sprintf("INSERT INTO %s.%s (`key`, value, expiry) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE `value`= ?, `expiry` = ?", s.database, s.table))
	if prepareErr != nil {
		return errors.Wrap(prepareErr, "failed to prepare write statement")
	}

	s.deletePrepare, prepareErr = s.db.Prepare(fmt.Sprintf("DELETE FROM %s.%s WHERE `key` = ?;", s.database, s.table))
	if prepareErr != nil {
		return errors.Wrap(prepareErr, "failed to prepare delete statement")
	}

	return nil
}

func (s *sqlStore) configure() error {
	nodes := s.options.Nodes
	if len(nodes) == 0 {
		nodes = []string{"localhost:3306"}
	}

	database := s.options.Database
	if len(database) == 0 {
		database = DefaultDatabase
	}

	table := s.options.Table
	if len(table) == 0 {

View on GitHub (pinned to 24529f1404)

Solutions

  1. Inspect the wrapped cause for the MySQL error code
  2. Validate database/table identifiers derived from the namespace
  3. Retry configure/recreate the store after connection recovery
  4. Check max_prepared_stmt_count and proxy restrictions on the server
Defensive patterns

Strategy: retry

Validate before calling

if err := db.Ping(); err != nil {
    return fmt.Errorf("mysql unreachable before store configure: %w", err)
}

Try / catch

if err := st.Init(opts...); err != nil {
    if strings.Contains(err.Error(), "failed to prepare delete statement") {
        time.Sleep(time.Second)
        return st.Init(opts...)
    }
    return err
}

Prevention

When it happens

Trigger: configure() -> initDB() prepares `DELETE FROM <db>.<table> WHERE key = ?` and db.Prepare errors, typically as the last of the three prepares after an earlier connection drop.

Common situations: MySQL restart or network blip mid-configure; invalid namespace characters in the table identifier; server-side prepared statement limits or restrictions reached.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/94fdf6f44c136d8f. Report an issue: GitHub.