Migrations fail with "duplicate key value violates unique constraint pg_class_relname_nsp_index"
SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "pg_class_relname_nsp_index"
DETAIL: Key (relname, relnamespace)=(migrations_id_seq, 2200) already exists.
(SQL: create table "migrations" ("id" serial not null primary key, ...))
- When
Scaffolding a new app into a directory an earlier, unfinished run used — or, on applications scaffolded before kalimera pinned
COMPOSE_PROJECT_NAME, into any directory whose name an earlier run used. The MySQL wording isSQLSTATE[42S01] ... Table 'migrations' already exists.- Why it happens
Compose files volumes under the project name as
<project>_sail-pgsql, and left to itself it takes that name from the directory. A new scaffold under a project name used before therefore mounts the old database. That volume can holdmigrations_id_seqwithout themigrationstable — Laravel'shasTable('migrations')returns false, issuescreate table migrations (id serial ...), and the implicit sequence collides with the orphan.- Fix
kalimera now handles this itself, in three places. A fresh scaffold pins
COMPOSE_PROJECT_NAME=<app>-<hash of the full path>in.env, so~/work/apiand~/side/apiare two projects to docker and can never share a database. Before the firstup,SailStartasks docker whether anything already answers to that name — the<project>-<service>-1containers and the<project>_sail-<service>volumes — which only an earlier run in this very directory can have left, and runssail down -vonly if something does, naming what it is about to remove. Nothing found means nothing touched, which is the usual case; a--continuerun keeps its data and is never asked. A resumed run whose.envpredates the key keeps the directory-name project its containers already use. If a conflict still gets through,AppFinalizerecreates the volume immediately rather than retrying three times first:MigrationFailureretries only when the database could not be reached (SQL class 08, MySQL 2002/2003/2006, or no output at all). Once the server has answered, its verdict will be the same on every attempt.To clear it by hand:
./vendor/bin/sail down -v && ./vendor/bin/sail up -d --wait && ./vendor/bin/sail artisan migrate
Still stuck? Open an issue with your --log transcript.