{"record":{"id":"1431a3a12a1355df","repo":"ory/hydra","slug":"problem-inserting-migration-version-s","errorCode":null,"errorMessage":"problem inserting migration version %s","messagePattern":"problem inserting migration version (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/popx/migrator.go","lineNumber":223,"sourceCode":"\t\t\tif slices.Contains(appliedMigrations, mi.Version) {\n\t\t\t\tl.Debug(\"Migration has already been applied, skipping.\")\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tif slices.Contains(appliedMigrations, legacyVersion) {\n\t\t\t\tl.WithField(\"legacy_version\", legacyVersion).WithField(\"migration_table\", mtn).Debug(\"Migration has already been applied in a legacy migration run. Updating version in migration table.\")\n\t\t\t\tif err := mb.isolatedTransaction(ctx, \"init-migrate\", func(conn *pop.Connection) error {\n\t\t\t\t\t// We do not want to remove the legacy migration version or subsequent migrations might be applied twice.\n\t\t\t\t\t//\n\t\t\t\t\t// Do not activate the following - it is just for reference.\n\t\t\t\t\t//\n\t\t\t\t\t// if _, err := tx.Store.Exec(fmt.Sprintf(\"DELETE FROM %s WHERE version = ?\", mtn), legacyVersion); err != nil {\n\t\t\t\t\t//\treturn errors.Wrapf(err, \"problem removing legacy version %s\", mi.Version)\n\t\t\t\t\t// }\n\n\t\t\t\t\t// #nosec G201 - mtn is a system-wide const\n\t\t\t\t\terr := conn.RawQuery(fmt.Sprintf(\"INSERT INTO %s (version) VALUES (?)\", mtn), mi.Version).Exec()\n\t\t\t\t\treturn errors.Wrapf(err, \"problem inserting migration version %s\", mi.Version)\n\t\t\t\t}); err != nil {\n\t\t\t\t\treturn errors.WithStack(err)\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tl.Info(\"Migration has not yet been applied, running migration.\")\n\n\t\t\tif err := mi.Valid(); err != nil {\n\t\t\t\treturn errors.WithStack(err)\n\t\t\t}\n\n\t\t\tnoTx := mb.shouldNotUseTransaction(mi)\n\t\t\tif noTx {\n\t\t\t\tl.Info(\"NOT running migrations inside a transaction\")\n\t\t\t\tif err := mi.Runner(mi, c); err != nil {\n\t\t\t\t\treturn errors.WithStack(err)\n\t\t\t\t}","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/popx/migrator.go#L205-L241","documentation":"During UpTo, when a migration was already applied under the legacy 14-char version format, the migrator inserts a new full-length version row inside an 'init-migrate' transaction so the migration is tracked under the current scheme. This error wraps a failure of that INSERT into the schema_migration table. It usually means the migration tracking table is unreachable, locked, or violates constraints.","triggerScenarios":"Running migrations Up while the schema_migration table contains the legacy truncated (14-char) version of a migration, and the INSERT of the full-length version fails (table missing, dropped, locked, unique index violation, permissions, connection dropped).","commonSituations":"Databases upgraded from older versions of the library that stored truncated timestamps; a manually emptied or renamed schema_migration table; DB user lacking INSERT privileges; connection dropped mid-migration.","solutions":["Check the schema_migration table exists and the DB user has INSERT privileges on it","Inspect the wrapped cause for the real SQL error (e.g. unique violation, table missing) and fix accordingly","Re-run migrations; the operation is idempotent because the legacy row is intentionally kept","If a duplicate row is the cause, manually remove the conflicting row before re-running"],"exampleFix":"// before (diagnostic)\n// SELECT * FROM schema_migration WHERE version LIKE '20221012%';\n// after: remove conflicting duplicate then re-run\n// DELETE FROM schema_migration WHERE version = '20221012101010';","handlingStrategy":"try-catch","validationCode":"var n int\nif err := db.QueryRow(\"SELECT COUNT(*) FROM schema_migration\").Scan(&n); err != nil {\n    // table missing/unreadable — run migrations init first\n}","typeGuard":null,"tryCatchPattern":"if err := box.Up(ctx, -1); err != nil {\n    if strings.Contains(err.Error(), \"problem inserting migration version\") {\n        // inspect wrapped cause, verify schema_migration table and privileges, reconcile, retry\n    }\n    return err\n}","preventionTips":["Never drop or rename the schema_migration table manually","Grant the migration DB user INSERT/SELECT on the tracking table","Upgrade from legacy truncated versions by running the migrator, not manual SQL"],"tags":["database","migration","go"],"backgroundTag":"migration-version-insert-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"}