Skip to content

fix(migrations): use INTEGER for sessions.user_id to match users.id FK type#311

Merged
dkhalife merged 2 commits intomainfrom
fix/sessions-fk-mysql
Apr 26, 2026
Merged

fix(migrations): use INTEGER for sessions.user_id to match users.id FK type#311
dkhalife merged 2 commits intomainfrom
fix/sessions-fk-mysql

Conversation

@dkhalife
Copy link
Copy Markdown
Owner

Problem

Migration 7 (sessions) fails on MySQL/MariaDB with:

```nError 1005 (HY000): Can't create table tasks.`sessions` (errno: 150 "Foreign key constraint is incorrectly formed")


On an affected deployment, `users.id` is `bigint(20)` (MariaDB), while migration 7 declared `sessions.user_id` as `INT` — type mismatch causes the FK to be rejected.

## Fix

- Use `INTEGER` for `sessions.id` and `sessions.user_id` (matches `users.id` declaration in migration 1, same pattern used by `labels.created_by` and `tasks.created_by` which FK successfully).
- Name the FK constraint (`fk_users_sessions`) for parity with other tables.
- Create indexes as separate statements after `CREATE TABLE` (matches the working `app_tokens` migration pattern).

No schema changes for SQLite path.

…K type

The MySQL/MariaDB sessions migration declared user_id as INT, but users.id
is created as INTEGER (which can map to BIGINT on MariaDB), causing FK
errno 150 on deployments where users.id was promoted to BIGINT.

Align with the pattern used elsewhere (labels, tasks): use INTEGER for the
FK column, name the constraint, and create indexes as separate statements
(matching the working app_tokens migration).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 26, 2026 17:13
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

❌ Patch coverage is 0% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
apiserver/internal/migrations/007_sessions.go 0.00% 23 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates migration 007 (sessions table) to improve MySQL/MariaDB compatibility by aligning column declarations and DDL structure with the patterns used elsewhere in the migration set.

Changes:

  • Refactors the MySQL sessions migration to execute CREATE TABLE plus index creation as separate statements.
  • Adjusts MySQL column types for sessions.id / sessions.user_id and names the users FK constraint (fk_users_sessions).
Show a summary per file
File Description
apiserver/internal/migrations/007_sessions.go Refactors MySQL sessions table creation, adds explicit FK constraint name, and creates indexes via separate statements.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread apiserver/internal/migrations/007_sessions.go Outdated
INTEGER and INT are synonyms in MySQL/MariaDB, so they don't resolve a
mismatch with users.id when it's actually BIGINT (e.g. on deployments
where users was originally created by GORM AutoMigrate). Query
information_schema.COLUMNS to derive the exact column type of users.id
and reuse it for sessions.id and sessions.user_id, ensuring the FK is
accepted on both fresh installs and legacy schemas.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dkhalife dkhalife merged commit 34343c0 into main Apr 26, 2026
6 of 7 checks passed
@dkhalife dkhalife deleted the fix/sessions-fk-mysql branch April 26, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants