Skip to content

Cannot run the migrations as of recently. Violates unique constraint "schema_migrations_pkey" #4417

@djandreski

Description

@djandreski

Describe the bug
As of recently I cannot seem to run the migrations to my local database. It always ends up with this error.

ERROR: duplicate key value violates unique constraint "schema_migrations_pkey" (SQLSTATE 23505)
Key (version)=(0001) already exists.
At statement: 18
INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES($1, $2, $3)

I debug what is obvious:

  • There is only one 0001_ migration to be run
  • There isn't any insert logic in there. In fact if I remove the statement 18, it will fail at the statement 17, if I keep removing code it doesn't matter it always fails at the end.
  • Stopped the instance, removed the backup, removed all the Docker volumes, images everything and start fresh. Now it fails at the supabase start command when it tries to run the migrations
  • I even uninstall Docker Desktop and did a fresh install
  • It is pretty wired, I start clean, no linked project and the output says that the objects from the migration already exists
Starting database...
Initialising schema...
Seeding globals from roles.sql...
Applying migration 0001_initial.sql...
NOTICE (42710): extension "uuid-ossp" already exists, skipping
NOTICE (42710): extension "pgcrypto" already exists, skipping
NOTICE (42P07): relation "users" already exists, skipping
NOTICE (42P07): relation "projects" already exists, skipping
NOTICE (42P07): relation "idx_projects_user_id" already exists, skipping
NOTICE (42P07): relation "tickets" already exists, skipping
NOTICE (42P07): relation "idx_tickets_project_id" already exists, skipping
NOTICE (42P07): relation "idx_tickets_parent_ticket_id" already exists, skipping
NOTICE (42P07): relation "deliverables" already exists, skipping
NOTICE (42P07): relation "idx_deliverables_project_id" already exists, skipping
NOTICE (42P07): relation "comments" already exists, skipping
NOTICE (42P07): relation "idx_comments_deliverable_id" already exists, skipping
NOTICE (42P07): relation "idx_comments_ticket_id" already exists, skipping
NOTICE (42P07): relation "api_keys" already exists, skipping
NOTICE (42P06): schema "private" already exists, skipping
Stopping containers...
ERROR: duplicate key value violates unique constraint "schema_migrations_pkey" (SQLSTATE 23505)
Key (version)=(0001) already exists.
At statement: 18
INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES($1, $2, $3)
  • I removed any reference to my remote instance from all .env files, but that didn't helped.
  • I checkout a commit, from when the migrations work, they aren't passing now too
  • I copy the migrations code in the supabase studio sql editor one by one, and they are working just fine
  • At one point I had a 0000_ migration that deletes everything from supabase_migrations.schema_migrations. And migrations sort of passed this way. But, when I opened the supabase studio, I didn't see the tables in the public schema, and there was also a missing supabase_migrations schema

It seems to me like the supabase command is working with some other instance somehow, or there is some cache left in my project that needs to be purged. But I don't know how. Or maybe the remote instance causing troubles?

To Reproduce
Steps to reproduce the behavior:
I'm not sure if this is a general bug it's just my setup

Expected behavior
When I run supabase db reset to run all the migrations and only write the new ones in supabase_migrations.schema_migrations

System information
Windows 11 with Docker Desktop Version 4.50.0 (209931)
Tried in WSL Ubuntu 24.04.2 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64) it is the same
All the supabase variants, standalone, pnpm, npx @latest @beta it is the same eveytime
Rerun the failing command with --create-ticket flag.
Sent crash report:

edabf23f7d8f424d9248e04c2a2dc5e2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions