dbal: Validation Error in doctrine:schema:validate After Merging PR #6418
Bug Report
| Q | A |
|---|---|
| Version | 3.8.5 and 4.0.3 |
Summary
The PR https://github.com/doctrine/dbal/pull/6418 breaks doctrine:schema:validate and prevents validation, suggesting replacing all CONSTRAINT definitions, especially those with the FOREIGN KEY.
Removing the problematic line resolves the issue: https://github.com/doctrine/dbal/blob/8edbce73bc1aa2251ba8c754fc440f8e02c661bc/src/Schema/Comparator.php#L374-L376
Current behaviour
doctrine:schema:validate fails validation and suggests replacing all foreign key CONSTRAINT definitions that include the FOREIGN KEY.
How to reproduce
Run doctrine:schema:validate
Observe the validation errors related to foreign key CONSTRAINT definitions with FOREIGN KEY
Example configuration causing the issue:
CREATE TABLE "cp_packages" (
"id_package" UUID NOT NULL,
"name" VARCHAR(128) NOT NULL,
"package_type" VARCHAR(512) NOT NULL,
CONSTRAINT "pk_cp_packages" PRIMARY KEY ("id_package")
)
CREATE TABLE "cp_package_virtual_machines" (
"id_package" UUID NOT NULL,
-- some attributes
CONSTRAINT "pk_cp_package_virtual_machines" PRIMARY KEY ("id_package"),
CONSTRAINT "id_package_cp_package_virtual_machines" FOREIGN KEY ("id_package")
REFERENCES "cp_packages"("id_package") ON DELETE CASCADE
)
validation schema errors
Mapping
-------
[OK] The mapping files are correct.
Database
--------
[ERROR] The database schema is not in sync with the current mapping file.
ALTER TABLE cp_package_virtual_machines DROP CONSTRAINT id_package_cp_package_virtual_machines;
ALTER TABLE cp_package_virtual_machines ADD CONSTRAINT FK_D8247456F5CEE5 FOREIGN KEY (id_package) REFERENCES cp_packages (id_package) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
It also completely ignores the Re-tested that is not true.ON DELETE RESTRICT rule, completely removing it.
Expected behaviour
doctrine:schema:validate should successfully validate the schema without suggesting changes to CONSTRAINT definitions, including those with the FOREIGN KEY
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 17 (10 by maintainers)
This change has also made 3.8.5 a breaking release across all our projects, which is unfortunate.
We run a database schema validation as part of our CI flow, after applying database migrations, to ensure that the migrated database is in sync with the doctrine schema tagging. These are all now failing because foreign key names don’t match.
The mismatches are for one of two reasons:
ALTER TABLE widgets DROP FOREIGN KEY FK_9D58E4C1EAD0F67C; ALTER TABLE widgets ADD CONSTRAINT FK_9D58E4C1FF9FC001 FOREIGN KEY (access_key_id) REFERENCES access_keys (id);)I can see there might be a purist argument for tracking the names as well as the definitions, and I don’t object to that in principle (especially since index names are validated).
However, I don’t think that can be made mandatory in a point release unless there’s a way for users to solve the validation failure by customising the doctrine schema so the expected name matches the existing database.
Certainly for mysql, a Foreign Key can only be renamed by dropping it and recreating it, which is potentially both an expensive and risky migration to run and I’m really not keen to do that across all our tables and projects. Particularly since evidently doctrine’s auto-naming strategy has already changed at some point in the past, and could change again in future, and it feels wrong if users are forced to change existing database schemas if & when that happens.
In my opinion, assuming ORM don’t have immediate plans to support customising FK names, this behaviour should either be reverted or made opt-in in the next 3.8.x point release.
@greg0ire I reverted it for 3.8.x here
No, we’ll just merge up the revert