Skip to content

Introduce PrimaryKeyConstraint #6867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 23, 2025
Merged

Conversation

morozov
Copy link
Member

@morozov morozov commented Mar 23, 2025

Currently, the primary key constraint is represented in DBAL as an index with the "primary" flag. This leads to various sorts of issues, for example:

  1. It is possible to declare a primary key constraint with the properties of an index that don't make sense for a primary key (e.g. declare it as partial, full-text, etc.), which is invalid and will fail at runtime.
  2. It is very challenging to add the support for primary constraint name (Use the provided index name for primary keys #3890).
  3. Because of that, dropping a primary key constraint is not implemented on a half of the supported database platforms.
  4. Table#renameIndex() likely doesn't work for primary key constraints.

This PR deprecates indexes with the "primary" flag in favor of PrimaryKeyConstraint.

Technical details

  1. The setPrimaryKey() method is being deprecated in favor of Table::addPrimaryKeyConstraint(). The "add" verb better corresponds to the SQL syntax (ALTER TABLE <name> ADD CONSTRAINT). The currently existing "set" method already has the "add" semantics (a PK cannot be set twice).
  2. The dropPrimaryKey() is not being deprecated. Even though there's now a slight inconsistency in naming, eventually Table will become immutable, so it will be deprecated. This way, I'm reducing the number of deprecations.
  3. Similar to Introduce equals() for names and foreign key constraints #6866, PrimaryKeyConstraint#equals() will be added in 5.0.x, not here, since it's not necessary for the upgrade and isn't always possible to use.

morozov added 4 commits March 22, 2025 10:53
Currently, if the name of the duplicate PK matches the name of the
existing one, an IndexAlreadyExists exception will be thrown. A
PrimaryKeyAlreadyExists exception will be thrown only if the name
doesn't match.

We want to reorder the checks to ihave PrimaryKeyAlreadyExists thrown
consistently.
@morozov morozov merged commit 83174f5 into doctrine:4.3.x Mar 23, 2025
68 checks passed
@morozov morozov deleted the primary-key-constraint branch March 23, 2025 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants