[GH-ISSUE #4787] [Bug]: #3322

Closed
opened 2026-10-31 20:31:38 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @MaNixBlacks on GitHub (Jun 2, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4787

Describe the bug

Database migration is not occurring on version 3.2.5.

How to reproduce the problem

update to 3.2.5
x-ui
27
2

INF] Stopping panel to take a consistent snapshot...

[INF] Migrating data into PostgreSQL...
2026/06/02 01:14:25 Creating destination schema...
2026/06/02 01:14:25 User 1 rows
2026/06/02 01:14:25 Setting 5 rows
2026/06/02 01:14:25 HistoryOfSeeders 6 rows
2026/06/02 01:14:25 CustomGeoResource 0 rows
2026/06/02 01:14:25 Node 0 rows
2026/06/02 01:14:25 ApiToken 0 rows
2026/06/02 01:14:25 Inbound 1 rows
2026/06/02 01:14:26 ClientTraffic 19730 rows
2026/06/02 01:14:26 OutboundTraffics 0 rows
2026/06/02 01:14:26 InboundClientIps 0 rows
2026/06/02 01:14:26 ClientRecord 19730 rows
migration failed: copy *model.ClientInbound: primary key required
[ERR] Migration failed. The panel was NOT switched to PostgreSQL.
1. Install PostgreSQL (server + client + xui db)
2. Migrate SQLite -> PostgreSQL
3. Status (clusters & port 5432)
4. Start PostgreSQL
5. Stop PostgreSQL
6. Restart PostgreSQL
7. Enable Autostart on boot
8. View PostgreSQL Log
0. Back to Main Menu
Choose an option:

20 000 clients error migration ==((

Expected behavior

No response

Actual behavior

No response

Relevant logs


Screenshots

No response

3x-ui version

3.2.5

Xray-core version

No response

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 24,04

Which parts of the panel are affected?

Database / migrations

Browser (only if it is a UI bug)

No response

Is the panel behind a reverse proxy or CDN?

None

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (or have verified the bug still exists on it).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, client UUIDs).
Originally created by @MaNixBlacks on GitHub (Jun 2, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/4787 ### Describe the bug Database migration is not occurring on version 3.2.5. ### How to reproduce the problem update to 3.2.5 x-ui 27 2 INF] Stopping panel to take a consistent snapshot... [INF] Migrating data into PostgreSQL... 2026/06/02 01:14:25 Creating destination schema... 2026/06/02 01:14:25 User 1 rows 2026/06/02 01:14:25 Setting 5 rows 2026/06/02 01:14:25 HistoryOfSeeders 6 rows 2026/06/02 01:14:25 CustomGeoResource 0 rows 2026/06/02 01:14:25 Node 0 rows 2026/06/02 01:14:25 ApiToken 0 rows 2026/06/02 01:14:25 Inbound 1 rows 2026/06/02 01:14:26 ClientTraffic 19730 rows 2026/06/02 01:14:26 OutboundTraffics 0 rows 2026/06/02 01:14:26 InboundClientIps 0 rows 2026/06/02 01:14:26 ClientRecord 19730 rows migration failed: copy *model.ClientInbound: primary key required [ERR] Migration failed. The panel was NOT switched to PostgreSQL. 1. Install PostgreSQL (server + client + xui db) 2. Migrate SQLite -> PostgreSQL 3. Status (clusters & port 5432) 4. Start PostgreSQL 5. Stop PostgreSQL 6. Restart PostgreSQL 7. Enable Autostart on boot 8. View PostgreSQL Log 0. Back to Main Menu Choose an option: 20 000 clients error migration ==(( ### Expected behavior _No response_ ### Actual behavior _No response_ ### Relevant logs ```shell ``` ### Screenshots _No response_ ### 3x-ui version 3.2.5 ### Xray-core version _No response_ ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 24,04 ### Which parts of the panel are affected? Database / migrations ### Browser (only if it is a UI bug) _No response_ ### Is the panel behind a reverse proxy or CDN? None ### Before submitting - [x] I searched [existing issues](../issues?q=is%3Aissue) and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (or have verified the bug still exists on it). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, client UUIDs).
bsakaguchi 2026-10-31 20:31:38 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jun 2, 2026):

Thank you for the detailed report!

The error 'migration failed: copy *model.ClientInbound: primary key required' points to a failure when copying the client_inbounds junction table (which links clients to inbounds). This table uses a composite primary key (client_id + inbound_id), and GORM's batch-insert rejects records where either key field is zero-valued.

With 19,730 clients this is a realistic scenario — some client_inbounds rows in your SQLite database likely have a zero value in one of those columns (e.g. an orphaned or unresolved client link), causing the whole batch to be rejected.

To diagnose, run this against your SQLite database before migrating:

sqlite3 /etc/x-ui/x-ui.db "SELECT COUNT(\*) FROM client_inbounds WHERE client_id = 0 OR inbound_id = 0;"

If the count is > 0, those rows are the cause.

Workaround to unblock yourself:

sqlite3 /etc/x-ui/x-ui.db "DELETE FROM client_inbounds WHERE client_id = 0 OR inbound_id = 0;"

Then retry the migration. This removes orphaned link rows that have no valid client or inbound reference — they should not affect actual traffic or client configuration.

This looks like a bug in the migration code that should skip or handle zero-value primary key rows gracefully rather than failing. Tagging for developer review.

<!-- gh-comment-id:4597871570 --> @github-actions[bot] commented on GitHub (Jun 2, 2026): Thank you for the detailed report\! The error 'migration failed: copy \*model.ClientInbound: primary key required' points to a failure when copying the client_inbounds junction table (which links clients to inbounds). This table uses a composite primary key (client_id + inbound_id), and GORM's batch-insert rejects records where either key field is zero-valued. With 19,730 clients this is a realistic scenario — some client_inbounds rows in your SQLite database likely have a zero value in one of those columns (e.g. an orphaned or unresolved client link), causing the whole batch to be rejected. **To diagnose, run this against your SQLite database before migrating:** ```bash sqlite3 /etc/x-ui/x-ui.db "SELECT COUNT(\*) FROM client_inbounds WHERE client_id = 0 OR inbound_id = 0;" ``` If the count is > 0, those rows are the cause. **Workaround to unblock yourself:** ```bash sqlite3 /etc/x-ui/x-ui.db "DELETE FROM client_inbounds WHERE client_id = 0 OR inbound_id = 0;" ``` Then retry the migration. This removes orphaned link rows that have no valid client or inbound reference — they should not affect actual traffic or client configuration. This looks like a bug in the migration code that should skip or handle zero-value primary key rows gracefully rather than failing. Tagging for developer review.
Author
Owner

@MHSanaei commented on GitHub (Jun 2, 2026):

Fixed.

Root cause: the SQLite→PostgreSQL copy used gorm's FindInBatches, which pages between batches using a single "prioritized" primary field. client_inbounds has a composite primary key (client_id + inbound_id) with no surrogate id and no auto-increment, so gorm leaves its PrioritizedPrimaryField nil and returns ErrPrimaryKeyRequired. The check only triggers once a table returns a full batch (>500 rows), which is why it reproduces on large installs and fails right after ClientRecord.

This is unrelated to zero-value/orphaned rows — gorm.Create does not raise that error and client_inbounds has no FK constraints.

Fix: copyTable now pages with explicit LIMIT/OFFSET ordered by each model's primary-key columns instead of FindInBatches. It handles composite-key tables, keeps memory bounded, and changes no schema. Added a Postgres-backed regression test that migrates a >500-row composite-key table.

Verified in Docker (golang:1.26-alpine + postgres:16-alpine): the seeded 600-row client_inbounds table now migrates cleanly where it previously failed with copy *model.ClientInbound: primary key required.

<!-- gh-comment-id:4598160276 --> @MHSanaei commented on GitHub (Jun 2, 2026): Fixed. **Root cause:** the SQLite→PostgreSQL copy used gorm's `FindInBatches`, which pages between batches using a single "prioritized" primary field. `client_inbounds` has a *composite* primary key (`client_id` + `inbound_id`) with no surrogate `id` and no auto-increment, so gorm leaves its `PrioritizedPrimaryField` nil and returns `ErrPrimaryKeyRequired`. The check only triggers once a table returns a full batch (>500 rows), which is why it reproduces on large installs and fails right after `ClientRecord`. This is unrelated to zero-value/orphaned rows — `gorm.Create` does not raise that error and `client_inbounds` has no FK constraints. **Fix:** `copyTable` now pages with explicit `LIMIT`/`OFFSET` ordered by each model's primary-key columns instead of `FindInBatches`. It handles composite-key tables, keeps memory bounded, and changes no schema. Added a Postgres-backed regression test that migrates a >500-row composite-key table. Verified in Docker (golang:1.26-alpine + postgres:16-alpine): the seeded 600-row `client_inbounds` table now migrates cleanly where it previously failed with `copy *model.ClientInbound: primary key required`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#3322
No description provided.