Delete with related field query fails due to invalid SQL generation
See original GitHub issueDescribe the bug Using a filter query to select rows for deletion fails if the filter query is a related field query (i.e. across a foreign key).
To Reproduce
class Device(Model):
device_id = fields.IntField(pk=True)
name = fields.CharField(max_length=16)
class LogMessage(Model):
device = fields.ForeignKeyFIeld('models.Device', related_name='log_messages')
date_created = fields.DatetimeField(auto_now_add=True)
message = fields.CharField(max_length=1024)
# [...]
await LogMessage.filter(device__name="test").delete()
Expected behavior In the example above, all “LogMessage” rows referencing devices having the name “test” should be deleted.
Instead, an invalid SQL DELETE query is generated.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
SQL Server deleting records with invalid query - Stack Overflow
I recently ran a query in SQL Server with invalid SQL included in the statement that inadvertently deleted all records in a table....
Read more >The DELETE statement conflicted with the REFERENCE ...
I have a two tables static_data_value and calc_invoice_volume Pk-FK relationship with on delete no action defined during table creation.
Read more >SQL error messages and exceptions - Oracle Help Center
An attempt to update or delete an already deleted row was made: No row was updated or deleted. 01003, Null values were eliminated...
Read more >SQL Error Messages | InterSystems IRIS Data Platform 2022.2
Table of SQL Error Codes and Messages ; -18, IS (or IS NOT) NULL predicate can be applied only to a field ;...
Read more >Error message reference - Prisma
Prisma Client throws a PrismaClientUnknownRequestError exception if the query engine returns an error related to a request that does not have an error...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I actually haven’t had time to look into this any further, but it may require the ForeignKeyField to be nullable as well, and I should mention this happens with PostgreSQL specifically. In any case, a query like
DELETE FROM LogMessage x LEFT JOIN Device y
was generated, and as far as I can tell, it should beDELETE x FROM LogMessage x LEFT JOIN Device y
orDELETE LogMessage FROM LogMessage LEFT JOIN Device
(i.e., specify specifically which table is being deleted from). Hope that helps.Hi guys, any news about the resolution of this issue?