{"record":{"id":"67d6aa084dbf983d","repo":"ory/hydra","slug":"unable-to-execute-statement-s","errorCode":null,"errorMessage":"unable to execute statement: %s","messagePattern":"unable to execute statement: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/popx/migrator.go","lineNumber":502,"sourceCode":"\t\tvar cancel context.CancelFunc\n\t\tctx, cancel = context.WithTimeoutCause(ctx, mb.perMigrationTimeout, errors.Errorf(\"failed to run all SQL migrations: direction=%s timeout=%s\", direction, mb.perMigrationTimeout))\n\t\tdefer cancel()\n\t}\n\n\treturn Transaction(ctx, mb.c.WithContext(ctx), func(ctx context.Context, connection *pop.Connection) error {\n\t\treturn errors.WithStack(fn(connection))\n\t})\n}\n\nfunc (mb *MigrationBox) createMigrationStatusTableTransaction(ctx context.Context, transactions ...[]string) error {\n\tfor _, statements := range transactions {\n\t\t// CockroachDB does not support transactional schema changes, so we have to run\n\t\t// the statements outside of a transaction. The same applies to any dialect\n\t\t// that opts into autocommit DDL (see noTxDDL).\n\t\tif mb.noTxDDL() {\n\t\t\tfor _, statement := range statements {\n\t\t\t\tif err := mb.c.WithContext(ctx).RawQuery(statement).Exec(); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"unable to execute statement: %s\", statement)\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif err := mb.isolatedTransaction(ctx, \"init\", func(conn *pop.Connection) error {\n\t\t\t\tfor _, statement := range statements {\n\t\t\t\t\tif err := conn.WithContext(ctx).RawQuery(statement).Exec(); err != nil {\n\t\t\t\t\t\treturn errors.Wrapf(err, \"unable to execute statement: %s\", statement)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn nil\n\t\t\t}); err != nil {\n\t\t\t\treturn errors.WithStack(err)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/popx/migrator.go#L484-L520","documentation":"This error wraps a raw SQL failure that occurred while creating or migrating the schema_migration bookkeeping table when the dialect opts into autocommit DDL (noTxDDL, e.g. CockroachDB). In that mode each DDL statement of the workload is executed directly with RawQuery(...).Exec() outside a transaction, and any statement error is wrapped as 'unable to execute statement: %s' with the offending SQL. The wrapped inner error contains the actual database reason (syntax, permissions, object state).","triggerScenarios":"createMigrationStatusTableTransaction, called from createTransactionalMigrationTable or migrateToTransactionalMigrationTable, executes one of the generated DDL statements (CREATE TABLE schema_migration, CREATE INDEX, INSERT, ALTER TABLE ... RENAME, DROP INDEX) via mb.c.WithContext(ctx).RawQuery(statement).Exec() and the database rejects it.","commonSituations":"The migration table or a legacy _pop_legacy table already exists from a partially failed prior upgrade (e.g. after an interrupted migrateToTransactionalMigrationTable); the database user lacks DDL privileges (CREATE/ALTER/DROP); a left-over index with the same name; connection loss mid-workload leaving inconsistent state on autocommit dialects.","solutions":["Read the wrapped inner error to identify the exact SQL statement and database reason.","Inspect the schema_migration table state; if a previous upgrade half-applied (interim *_transactional or *_pop_legacy tables exist), manually reconcile or drop the leftovers and retry.","Grant the database user the required DDL privileges (CREATE, ALTER, DROP, INDEX) on the schema.","Check connectivity and retry CreateSchemaMigrations, which is idempotent once conflicting objects are cleaned up."],"exampleFix":"// before: partial state from failed upgrade\nDROP TABLE schema_migration_pop_legacy;\nDROP TABLE schema_migration_transactional;\nDROP TABLE schema_migration;\n// after: retry migration init on a clean slate\nmb.CreateSchemaMigrations(ctx)","handlingStrategy":"validation","validationCode":"// validate DDL privileges and table state before initializing\nif err := c.RawQuery(\"SELECT 1 FROM schema_migration LIMIT 1\").Exec(); err != nil {\n    log.Printf(\"schema_migration state check: %v — cleanup legacy tables before init\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := mb.CreateSchemaMigrations(ctx); err != nil {\n    var wrapped interface{ Cause() error }\n    if strings.Contains(err.Error(), \"unable to execute statement:\") {\n        log.Printf(\"DDL failed (autocommit dialect): %v\", err) // reconcile schema_migration state manually\n    }\n    return err\n}","preventionTips":["Grant the app DB user full DDL rights (CREATE/ALTER/DROP) on its schema.","Never leave partially applied legacy *_pop_legacy / *_transactional tables after a failed upgrade.","Run migration init against the same dialect you will run migrations with.","Back up the schema_migration table before version upgrades of the migrator."],"tags":["database","migration","sql","ddl"],"backgroundTag":"sql-statement-execution-failed","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}