{"record":{"id":"8a8a18bb177e2ce1","repo":"juanfont/headscale","slug":"creating-new-table-w","errorCode":null,"errorMessage":"creating new table: %w","messagePattern":"creating new table: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"hscontrol/db/db.go","lineNumber":378,"sourceCode":"  created_at datetime,\n  updated_at datetime,\n  deleted_at datetime,\n  CONSTRAINT fk_nodes_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,\n  CONSTRAINT fk_nodes_auth_key FOREIGN KEY(auth_key_id) REFERENCES pre_auth_keys(id)\n)`,\n\t\t\t\t\t\t`CREATE TABLE policies(\n  id integer PRIMARY KEY AUTOINCREMENT,\n  data text,\n  created_at datetime,\n  updated_at datetime,\n  deleted_at datetime\n)`,\n\t\t\t\t\t}\n\n\t\t\t\t\tfor _, createSQL := range tableCreationSQL {\n\t\t\t\t\t\terr := tx.Exec(createSQL).Error\n\t\t\t\t\t\tif err != nil {\n\t\t\t\t\t\t\treturn fmt.Errorf(\"creating new table: %w\", err)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Copy data directly using SQL\n\t\t\t\t\tdataCopySQL := []string{\n\t\t\t\t\t\t`INSERT INTO users (id, name, display_name, email, provider_identifier, provider, profile_pic_url, created_at, updated_at, deleted_at)\n             SELECT id, name, display_name, email, provider_identifier, provider, profile_pic_url, created_at, updated_at, deleted_at\n             FROM users_old`,\n\n\t\t\t\t\t\t`INSERT INTO pre_auth_keys (id, key, user_id, reusable, ephemeral, used, tags, expiration, created_at)\n             SELECT id, key, user_id, reusable, ephemeral, used, tags, expiration, created_at\n             FROM pre_auth_keys_old`,\n\n\t\t\t\t\t\t`INSERT INTO api_keys (id, prefix, hash, expiration, last_seen, created_at)\n             SELECT id, prefix, hash, expiration, last_seen, created_at\n             FROM api_keys_old`,\n\n\t\t\t\t\t\t`INSERT INTO nodes (id, machine_key, node_key, disco_key, endpoints, host_info, ipv4, ipv6, hostname, given_name, user_id, register_method, forced_tags, auth_key_id, last_seen, expiry, approved_routes, created_at, updated_at, deleted_at)","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/hscontrol/db/db.go#L360-L396","documentation":"The SQLite schema-recreation migration failed executing one of the hardcoded CREATE TABLE statements (users, pre_auth_keys, api_keys, nodes, policies) that define the v0.27.0 canonical schema. Since old tables were renamed to _old just before, a failure here means the new empty table could not be created: name collision from a prior failed run, locked database, or a leftover object with the same name.","triggerScenarios":"CREATE TABLE rejected because a table/view/index with that name still exists (inconsistent state from an aborted earlier attempt), the database is locked, or the file is read-only/full.","commonSituations":"Retrying the migration after a crash where the transaction rollback did not fully restore names; disk full during the rebuild.","solutions":["Restore from the backup taken before upgrading - half-rebuilt SQLite schemas are not safely repairable by hand.","Free disk space (the recreation needs room for old + new tables simultaneously).","Ensure no other connection holds locks, then retry once.","If retrying is unavoidable, verify via sqlite_master that no duplicate partially-created objects exist first."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := db.NewHeadscaleDatabase(cfg); err != nil {\n    if strings.Contains(err.Error(), \"creating new table\") {\n        // inconsistent state after a crashed run: restore backup rather than manual DDL\n    }\n}","preventionTips":["Keep the pre-upgrade backup until the server has started successfully at least once.","Ensure disk space for old + new copies of all tables during the rebuild.","After any failed attempt, restore the backup instead of retrying against a half-rebuilt schema."],"tags":["database","sqlite","migration","ddl","backup"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}