question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delete with related field query fails due to invalid SQL generation

See original GitHub issue

Describe 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:open
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
davidpursercommented, Feb 3, 2020

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 be DELETE x FROM LogMessage x LEFT JOIN Device y or DELETE LogMessage FROM LogMessage LEFT JOIN Device (i.e., specify specifically which table is being deleted from). Hope that helps.

0reactions
lewoudarcommented, Jul 20, 2021

Hi guys, any news about the resolution of this issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found