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
Add support for joins in delete and update queries #8777
Comments
You can do this by naming 2 tables in the |
Joins on DELETE/UPDATE are not consistent, and sometimes impossible across the various db engines we support. That is the the main reason I didn't add support originally. |
The description for this enhancement is just to broad. There are to many different scenarios where a delete with a join is a symptom of another problem. Database design can prevent this and triggers can delete dependencies. Can you give an example that applies to all db engines and can't be resolved by schema design or trigger? |
Here you can find a question at stackoverflow about an scenario where matching in delete statements would be needed. |
Using a subquery to do joins and filter the records to delete or update is how I would recommend doing this kind of work. It works across all database vendors, and doesn't require dialect specific SQL. |
In this case I think that's not possible because you can't launch a DELETE and a SELECT over the same table (MySQL restriction), so you can't use the target table in the subselect. The only solution I've found (not very beautiful) is encapsulating the subquery in another select, this way. Note that making a query to get the resulting IDs and creating a simple condition "IN (1,4,.....988)" is not a solution, 'cause if there were too many IDs the query would grow without control. I know this kind of query is not very ussual, so for me it's no problem to write one only plain SQL query in my whole application. What happens to me is I'm a bit surprised 'cause it seems this is the first time I find something I can't do with the ORM :) |
Even the best ORM has it's limitations :) |
@pperejon would MySQL actually let you delete rows when using a join? |
@lorenzo yes, http://stackoverflow.com/questions/8598791/sql-delete-with-inner-join |
I think it makes sense to implement it for |
Just to confirm it, as @raul338 said, with an script you can easily test:
btw, I realize now my question at stackoverflow is not correct. The case is not to remove all the departments where there is an employee named John, but the opposite, remove all employees from the TI department (or any condition on the related table). I'll fix it right now. |
@pperejon you can use a subquery for that, though |
My god!!! I was really confused. You're right @lorenzo. Not to make mistakes again I've tested it with a fresh Cake install. With the previous SQL schema, if you do this:
...you'll get this query in the mysql log:
..which is perfectly correct for me. The issue about launching DELETE queries with the matching function may still be opened, but definetively it's not the case I was exposing. Sorry about that 😢 |
Tried
resulting in SQL could not be generated for this query as it is incomplete |
@mehov You are missing |
@ADmad Added it but getting the same thing I'm afraid. Full query:
Am I doing it wrong? |
This issue is stale because it has been open for 120 days with no activity. Remove the |
This is a (multiple allowed):
Currently there is no way to implement/use joins in either delete or update queries. Doing so will require a custom query compiler for at least Postgres which does not use a conventional "join" syntax.
The text was updated successfully, but these errors were encountered: