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
DDC-1098: Cascading delete is broken for all relationships other than inheritance relationships, at least on MySQL 5.0 #1693
Comments
Comment created by @beberlei: This is expected, cascade remove is working with in memory instances of the to be deleted objects. This way lifecycle events are triggered. If you want to perform this operation on the databse level you have to set onDelete="CASCADE" as an option of the join column: This difference should be explained in the docs on Working with Associations, 8.6 Transitive persistence / Cascade Operations i guess. |
Comment created by dalvarez: Good idea. I believe that it is normal to expect that declared cascade behaviours refer to persistent state, and not only to in-memory object graphs. That declared cascade behaviours are only applicable to objects in main memory is a fundamental limitation that
The current documentation states: "Persisting, removing, detaching and merging individual entities can become pretty cumbersome, especially when a large object graph with collections is involved. Therefore Doctrine 2 provides a mechanism for transitive persistence through cascading of these operations. Each association to another entity or a collection of entities can be configured to automatically cascade certain operations. By default, no operations are cascaded. The following cascade options exist: persist : Cascades persist operations to the associated entities. How are users supposed to understand, that the "remove" option is limited to in-memory object graphs, if all the docs say is "Cascades remove operations to the associated entities.". From a user standpoint, the entities are associated, and even the more so being persistent, aren't they? |
Comment created by dalvarez: Also, we obviously have a different understanding of the word "fixed". |
Comment created by @beberlei: Persist remove works, it fetches the association and deletes the objects, pulling them into memory. As to the ticket, i wanted to mark it as invalid and must have missclicked. |
Comment created by dalvarez: If it works, why is there an exception? The situation was pretty isolated. The entity to be deleted was merge()d, and then remove()d. "remove" was declared as a cascade option. Doctrine simply issued a "DELETE FROM \x where dbID = '1'", but did not remove the associated entity, instead violating the integrity constraint. |
Comment created by dalvarez: I think we are referring to the same end result: After all, persistent state needs to be modified one way or the other. Otherwise it would not make any sense. Still, the cascading delete does not work for me. It does not modify persistent state in any way. E. g. I have a use case where the following association is declared within an entity class, say A. /**** protected $b; Here, the cascade option "remove" is set. Now, if I remove a persistent instance of this class, like so: $doctrineEntityManager->merge($a); $doctrineEntityManager->remove($a); it will delete that particular entity instance from the database, but none of the entity instances associated with it through the association. I made sure that also "merge" was activated for the association - just in case it matters to be able to pull the object graph into memory first. Funnily enough, inheritance relationships declare a ON DELETE CASCADE at the database level. Just that all non-inheritance associations rely on that other in-memory mechanism, that, in the scenario above, does not work. I don't get it why the same mechanism cannot be used for both scenarios - after all its about deleting dependent database rows in both cases. Am I doing it wrongly? How is this mechanism supposed to work? Could you please verify it does work? (For me, it does not, at least not as described above). Thanks. |
Comment created by dalvarez: Oh yes, I forgot to mention I am now using version 2.1.1 of Doctrine. The last post was tested using version 2.1.1. |
Comment created by dalvarez: Updated the issue data to reflect the current environment. |
Comment created by @beberlei: Can you give an example domain for this bug? since it seems to have to do either with merge or inheritance. following questions:
|
Comment created by dalvarez: Thanks for your quick reply. I have now tested quite a number of different scenarios, both scenarios that involve inheritance, and scenarios that do not. Inheritance was just the matter in the original issue because the initial observation was that, at the database level, only inheritance relationships would declare an ON DELETE CASCADE constraint. Inheritance does not really have an effect on the basic problem that delete operations are not cascaded. As the application code I use involves a lot of inheritance, and since you explicitly mentioned it, I included inheritance in the tests, too, just to see if it made a difference. I performed the tests based on two different data models - first one where the target entity class of the association would inherit from another entity class, and a second one where it does not. In the end, inheritance did not matter. The results were the same whether the target entity inherits from a base class or not. What did matter is the way in which the delete was performed. I have accidentally given you wrong information about the way I actually deleted the entity instances in my last post. Since calling the remove method is the regular way I delete objects in my application, I blindly assumed it would be the same in this scenario. Unfortunately, I overlooked a special case here. In that case, the delete is performed using a DQL DELETE query. It seems that, for deletions performed using DQL queries, the cascade options are not respected. This is only logical, since - assuming that DQL gets compiled down straight to SQL and has no side effects on the in-memory objects - there will not be any in-memory object graph, and therefore any technique based purely on in-memory objects cannot possibly work for DQL. A solution would be to declare ON DELETE CASCADE constraints at the database level too (as already done in case of inheritance relationships) for associations that declare "remove" as a cascade option, so that the SQL resulting from the compilation of DQL DELETE queries will have the expected semantics, and maybe the onDelete annotation would be mandatory here. Here are the details of the tests I performed: First, here are the entity models used for the test: Here's the first, simple, model:
There is an entity class "Something" which refers to an entity class "SomethingElse" with a bidirectional one-to-one association Here is the second model, where the target entity class of the association inherits from another entitiy class.
I used the Doctrine command-line tools to generate the proxy classes and update the database model. Consequently, the following tables were generated: For the simple model:
And for the model involving inheritance:
Using doctrine, I then created some sample entity instances:
The database ends up with the following content: For the simple model:
And for the model involving inheritance:
We now have an entity instance of class "Something", which refers to an entity instance of class "SomethingElse". The database with the model involving inheritance is identical, except for the implementation detail that the entity instance of class "SomethingElse" inherits from an entity instance of class "BaseClassForSomethingElse". This is the setup. Now to the program code: This code deletes both entity instances correctly, as expected, including the base class row in case of the second data model:
This code will also work correctly, having the exact same effect as the code above:
However, this code will not work:
The code deletes the entity instance of class "Something", but will not delete the dependent data object of class "SomethingElse". It seems like the cascade option for the cascading delete is completely ignored by any delete operations performed through DQL queries (as opposed to e. g. calling the remove method on the entity manager). In the trivial example above, the query could simply be replaced by a call to the remove method, of course. Fortunately, in the application I am developing, this is possible, so I can resort to that. Is this behaviour (cascade options being bypassed by DQL queries) intended? At least it is logical according to your explanation of the in-memory object-graph approach. However, I found a mention of an "onDelete" option in the "transitive persistence" section of the reference documentation "[...] To rely on the database level cascade operations for the delete operation instead, you can configure each join column with the onDelete option. See the respective mapping driver chapters for more information." Do I have to rely on that, even if cascade="remove" is declared? The annotation reference explains the onDelete annotation as "onDelete: Cascade Action (Database-level)". However, this is misleading, because a cascade option is understood to be one of "persist"/"remove"/"detach"/"merge"/"all" throughout the annotation reference. Only the Doctrine 1.2 docs contain a mention of "database-level cascades" (http://www.doctrine-project.org/documentation/manual/1_1/ru/defining-models:transitive-persistence:database-level-cascades). Reading the source, I assume the syntax would be onDelete="cascade", but from the docs this is not evident. Personally, I would opt for generally using ON DELETE CASCADE constraints at the database level - either exclusively, or in addition to anything that might happen in the object domain. This is IMO the only efficient way to make the declared cascade={"remove"} constraint be generally effective. Please clarify if the behaviour is intended, and if declaring onDelete="cascade" is mandatory to have DQL queries cascade delete operations. Thanks. |
Comment created by dalvarez: Wrapped the code in code blocks and the MySQL tables into noformat-blocks. |
Comment created by @beberlei: Ah ok, using DELETE makes all the difference. This is actually the exepcted behavior. Cascade Operations are purely in-memory operations and dont trigger on DELETE statements. That is waht the join column on-delete definitions are for. This is documented in the DQL chapter, but should probably be documented in the cascade section aswell: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#delete-queries |
Comment created by dalvarez: Thanks for the clarification. Doctrine 2 really is one complex beast to tame. Maybe a note in the architecture section could point out that anything involving DQL belongs to the database domain as opposed to the object domain. As DQL is toutet as an object-query-language it is quite contrary to the principle of least astonishment that cascade constraints defined at the object-domain-level are ignored. Bypassing object-domain constraints basically makes DQL a mere SQL abstraction rather than a fully-blown object-query-language aware of the object domain constraints. This is a very fundamental architectural tenet. I will close this issue as invalid, because it does work as intended by the creators, even if the behaviour IMO constitutes a questionable design choice, being highly counter-intuitive without compensating for it by giving adequate other benefits to the user that would necessarily depend on that design choice. |
Comment created by dalvarez: Works as intended, though maybe not as to be expected. |
Issue was closed with resolution "Invalid" |
Jira issue originally created by user dalvarez:
The console tools do not generate the proper DDL statements for the constraints declared as part of the relationship annotations.
Take this entity field declaration, e.g. :
This declaration should generate some sort of ON DELETE CASCADE constraint, or at least actually cascade the delete, in whatever way. It does not. I reviewed the generated DDL statements with
And it clearly showed that no "ON DELETE CASCADE" was generated. Neither did Doctrine 2 perform a cascading delete otherwise.
The delete simply does not cascade.
Subsequently, I end up getting errors like:
Exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails
(
invoiceCreatorDB/AnalogOrderInvoiceLineItem
, CONSTRAINTAnalogOrderInvoiceLineItem*ibfk_1
FOREIGN KEY (partialInvoice*dbID
) REFERENCESPartialInvoice
(dbID
))'The query that caused the error is:
(PartialInvoice is the entity containing the above field declaration).
It seems that the cascading delete feature is fundamentally broken.
The text was updated successfully, but these errors were encountered: