dbal
dbal copied to clipboard
Foreign keys still created after preventDefault invoked for onSchemaCreateTable events
Bug Report
When trying to prevent a table from being created by calling $event->preventDefault()
in a listener for Events::onSchemaCreateTable
the create table statements are not present but any foreign key statements those tables would have needed are still generated. If those queries are run there is then a "table not found" result when running the foreign key statements..
Q | A |
---|---|
BC Break | no? |
Version | 2.13.6 (and it appears this would still happen in 3.2.0) |
Summary
On MySQL and probably any other platform that supportsForeignKeyConstraints
returns true for this occurs. Even though the event listener instructs the event to preventDefault
it still tries to create the foreign keys. This appears to be due to the loop in SchemaDiff.php ignoring the fact $platform->getCreateTableSQL(...)
returned an empty array since the event listener told it to not create the table.
Current behaviour
The update schema command is returning the following. If the event listener was not there, there'd also be a CREATE TABLE statement.
ALTER TABLE table_name ADD CONSTRAINT FK_B6C6A7F5BE04EA9 FOREIGN KEY (fk_id) REFERENCES table_name (id) ON DELETE CASCADE;
How to reproduce
-
Create a Doctrine entity that has a foreign key
-
Create an event listener for Events::onSchemaCreateTable
public function onSchemaCreateTable(SchemaCreateTableEventArgs $event): void
{
$event->preventDefault();
}
- Run the update schema command
Expected behaviour
[OK] Nothing to update - your database is already in sync with the current entity metadata.