Primary key conflict on fresh install of Immich #2081

Closed
opened 2026-02-05 05:01:53 +03:00 by OVERLORD · 5 comments
Owner

Originally created by @mmomjian on GitHub (Jan 30, 2024).

The bug

On a fresh run of Immich, I am getting a "duplicate key value violates unique constraint" error from Postgres at the very beginning of the logs for the immich container. Is anyone else able to replicate this same error? The issue does not persist after restarting an already initialized container - it is only present on the first run of a brand new instance, with a brand new UPLOAD_LOCATION and fresh (already existing database. Immich does not have superuser permission on the Postgres instance. Immich otherwise runs totally fine. Output below.

The OS that Immich Server is running on

Debian 12

Version of Immich Server

v1.93.3

Version of Immich Mobile App

n/a

Platform with the issue

  • Server
  • Web
  • Mobile

Your docker-compose.yml content

n/a

Your .env content

n/a

Reproduction steps

1. Create a new instance of Immich connected to a fresh Postgres database.
2. Run `docker logs immich`

Additional information

/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219
            throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
                  ^

QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
    at PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9)
    at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17)
    at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35)
    at async DatabaseRepository.runMigrations (/usr/src/app/dist/infra/repositories/database.repository.js:42:9)
    at async DatabaseService.init (/usr/src/app/dist/domain/database/database.service.js:33:9)
    at async AppService.init (/usr/src/app/dist/immich/app.service.js:56:9)
    at async AppModule.onModuleInit (/usr/src/app/dist/immich/app.module.js:32:9)
    at async callModuleInitHook (/usr/src/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9) {
  query: '\n' +
    '        create table if not exists users\n' +
    '        (\n' +
    '            id          uuid      default uuid_generate_v4() not null\n' +
    '                constraint "PK_a3ffb1c0c8416b9fc6f907b7433"\n' +
    '                    primary key,\n' +
    '            email       varchar                              not null,\n' +
    '            password    varchar                              not null,\n' +
    '            salt        varchar                              not null,\n' +
    '            "createdAt" timestamp default now()              not null\n' +
    '        );\n' +
    '      ',
  parameters: undefined,
  driverError: error: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
      at /usr/src/app/node_modules/pg/lib/client.js:526:17
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:184:25)
      at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9)
      at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17)
      at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35)
Originally created by @mmomjian on GitHub (Jan 30, 2024). ### The bug On a fresh run of Immich, I am getting a "duplicate key value violates unique constraint" error from Postgres at the very beginning of the logs for the `immich` container. Is anyone else able to replicate this same error? The issue does not persist after restarting an already initialized container - it is only present on the first run of a brand new instance, with a brand new UPLOAD_LOCATION and fresh (already existing database. Immich does not have superuser permission on the Postgres instance. Immich otherwise runs totally fine. Output below. ### The OS that Immich Server is running on Debian 12 ### Version of Immich Server v1.93.3 ### Version of Immich Mobile App n/a ### Platform with the issue - [X] Server - [ ] Web - [ ] Mobile ### Your docker-compose.yml content ```YAML n/a ``` ### Your .env content ```Shell n/a ``` ### Reproduction steps ```bash 1. Create a new instance of Immich connected to a fresh Postgres database. 2. Run `docker logs immich` ``` ### Additional information ``` /usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219 throw new QueryFailedError_1.QueryFailedError(query, parameters, err); ^ QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index" at PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9) at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17) at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35) at async DatabaseRepository.runMigrations (/usr/src/app/dist/infra/repositories/database.repository.js:42:9) at async DatabaseService.init (/usr/src/app/dist/domain/database/database.service.js:33:9) at async AppService.init (/usr/src/app/dist/immich/app.service.js:56:9) at async AppModule.onModuleInit (/usr/src/app/dist/immich/app.module.js:32:9) at async callModuleInitHook (/usr/src/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9) { query: '\n' + ' create table if not exists users\n' + ' (\n' + ' id uuid default uuid_generate_v4() not null\n' + ' constraint "PK_a3ffb1c0c8416b9fc6f907b7433"\n' + ' primary key,\n' + ' email varchar not null,\n' + ' password varchar not null,\n' + ' salt varchar not null,\n' + ' "createdAt" timestamp default now() not null\n' + ' );\n' + ' ', parameters: undefined, driverError: error: duplicate key value violates unique constraint "pg_type_typname_nsp_index" at /usr/src/app/node_modules/pg/lib/client.js:526:17 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:184:25) at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9) at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17) at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35) ```
Author
Owner

@jrasm91 commented on GitHub (Jan 30, 2024):

Both the immich-server and immich-microservices run database migrations when they start up. If you start them up at exactly the same time they sometimes run into this issue.

@jrasm91 commented on GitHub (Jan 30, 2024): Both the `immich-server` and `immich-microservices` run database migrations when they start up. If you start them up at exactly the same time they _sometimes_ run into this issue.
Author
Owner

@mmomjian commented on GitHub (Jan 30, 2024):

Yes, they are starting at the same time. Thanks for the insight. In my experience this happens nearly 100% of the time. Does this pose any threat to the health of the Immich instance that is created with both migrations taking place at the same time? I imported about 50k images into my instance and it is running fine but I will start over if needed.

@mmomjian commented on GitHub (Jan 30, 2024): Yes, they are starting at the same time. Thanks for the insight. In my experience this happens nearly 100% of the time. Does this pose any threat to the health of the Immich instance that is created with both migrations taking place at the same time? I imported about 50k images into my instance and it is running fine but I will start over if needed.
Author
Owner

@jrasm91 commented on GitHub (Jan 30, 2024):

No, because they each run the migrations sequentially and inside of transactions so it's never an issue, except that one aborts if the other is doing the same thing at the same time.

@jrasm91 commented on GitHub (Jan 30, 2024): No, because they each run the migrations sequentially and inside of transactions so it's never an issue, except that one aborts if the other is doing the same thing at the same time.
Author
Owner

@mmomjian commented on GitHub (Jan 30, 2024):

That makes sense. Thank you. Would you be interested in me adding a "tips" section to the docker compose docs page outlining that this is a harmless message?

@mmomjian commented on GitHub (Jan 30, 2024): That makes sense. Thank you. Would you be interested in me adding a "tips" section to the docker compose docs page outlining that this is a harmless message?
Author
Owner

@jrasm91 commented on GitHub (Jan 30, 2024):

Yeah, you can add a tip and/or a FAQ for this error, until we find a more reliable way to run the migrations without the concurrency issue.

@jrasm91 commented on GitHub (Jan 30, 2024): Yeah, you can add a tip and/or a FAQ for this error, until we find a more reliable way to run the migrations without the concurrency issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: immich-app/immich#2081