Developing software in the Real World

Changing an SQL Server primary key in Doctrine Migrations

I recently came across a rather weird quirk when trying to change a primary key in Sql Server using Doctrine Migrations: you need to use two migrations to get it to work.

This is incredibly minor and I’m only writing it up as it confused me for a while so I thought that I’d document so that I’ll find this article if I run across it again in the future!

This is the migration:

When you run it with SQL Server, you get this error:

The actual problem is that the primary key name of [primary] is incorrect. Something somewhere is losing the name of the current primary key ([PK__page_cat__E48D0CA0589C25F3]) because there’s a setPrimaryKey() call in the same migration.

If we split into two migrations:


Then it works as expected:

As you can see, it’s hardly a big problem to create two migrations to work around this and I’ve reported it to the project as issue 3736.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *