benbjohnson/litestream · error

create table %s: %w

Error message

create table %s: %w

What it means

populateDatabase wraps a failed CREATE TABLE for test_table_%d in this error. Each configured table is created with a fixed schema (data blob, text, int, float, timestamp columns) before rows are inserted. A failure means schema setup failed and no population for that table can proceed.

Source

Thrown at cmd/litestream-test/populate.go:107

		return fmt.Errorf("set synchronous: %w", err)
	}

	for i := 0; i < c.TableCount; i++ {
		tableName := fmt.Sprintf("test_table_%d", i)

		createSQL := fmt.Sprintf(`
			CREATE TABLE %s (
				id INTEGER PRIMARY KEY AUTOINCREMENT,
				data BLOB,
				text_field TEXT,
				int_field INTEGER,
				float_field REAL,
				timestamp INTEGER
			)
		`, tableName)

		if _, err := db.Exec(createSQL); err != nil {
			return fmt.Errorf("create table %s: %w", tableName, err)
		}

		if c.IndexRatio > 0 {
			if rand.Float64() < c.IndexRatio {
				indexSQL := fmt.Sprintf("CREATE INDEX idx_%s_timestamp ON %s(timestamp)", tableName, tableName)
				if _, err := db.Exec(indexSQL); err != nil {
					return fmt.Errorf("create index: %w", err)
				}
			}
			if rand.Float64() < c.IndexRatio {
				indexSQL := fmt.Sprintf("CREATE INDEX idx_%s_int ON %s(int_field)", tableName, tableName)
				if _, err := db.Exec(indexSQL); err != nil {
					return fmt.Errorf("create index: %w", err)
				}
			}
		}
	}

View on GitHub (pinned to 4ed7a308f6)

Solutions

  1. Drop existing test_table_* tables (or delete the database file) before re-running populate.
  2. Check free disk space on the database volume.
  3. Verify the wrapped SQLite error for 'table already exists' vs I/O errors.
  4. Ensure the database file is not opened read-only.

Example fix

// before
litestream-test populate -db data.db   // re-run, tables exist
// after
rm data.db data.db-wal data.db-shm && litestream-test populate -db data.db
Defensive patterns

Strategy: validation

Validate before calling

var exists int
if err := db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?", tableName).Scan(&exists); err != nil {
    return err
}
if exists > 0 {
    if _, err := db.Exec(fmt.Sprintf("DROP TABLE %s", tableName)); err != nil {
        return err
    }
}

Try / catch

if _, err := db.Exec(createSQL); err != nil {
    if strings.Contains(err.Error(), "already exists") {
        // drop and retry or skip
    }
    return fmt.Errorf("create table %s: %w", tableName, err)
}

Prevention

When it happens

Trigger: db.Exec(createSQL) fails: a table with the same name already exists with a different schema, SQL syntax issues from a malformed table name, disk I/O error, or the database is read-only.

Common situations: Re-running populate against a database from a previous run without dropping tables; TableCount names colliding with pre-existing objects; disk full on the volume hosting the database.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of benbjohnson/litestream@4ed7a308f6 (2026-09-06). Data as JSON: /api/errors/b97687393203caa0. Report an issue: GitHub.