Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ Changelog
1.1
===

1.1.7
1.1.8
-----

Fixed
^^^^^
- Fixed DELETE and UPDATE queries failing when filtering by related fields (foreign keys). Using a subquery pattern instead of JOIN for compatibility with MySQL and SQLite. (#283)

Added
^^^^^
- Tests for model validators. (#2137)

1.1.7
-----

Fixed
^^^^^
- Reorder delete model operations in migrations to avoid foreign key constraint errors. (#2145)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,29 @@ async def test_delete_limit_order_by(db, intfields_data):
await IntFields.get(intnum=97)


@pytest.mark.asyncio
async def test_delete_filter_with_foreign_key(db):
author = await Author.create(name="test")
await Book.create(name="book1", author=author, rating=5.0)
await Book.create(name="book2", author=author, rating=4.0)

# This is the failing query
await Book.filter(author__name="test").delete()

assert await Book.all().count() == 0


@pytest.mark.asyncio
async def test_update_filter_with_foreign_key(db):
author = await Author.create(name="test")
await Book.create(name="book1", author=author, rating=5.0)

await Book.filter(author__name="test").update(rating=1.0)

book = await Book.first()
assert book.rating == 1.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some test, that tests behavior in cases where there are limit and order by clauses, to see that subquery will update/delete only items that fall into subquery



@pytest.mark.asyncio
async def test_async_iter(db, intfields_data):
counter = 0
Expand Down
35 changes: 35 additions & 0 deletions tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,26 @@ def _make_query(self) -> None:
self.resolve_ordering(self.model, table, self._orderings, self._annotations)

self.resolve_filters()
if self._joined_tables:
# If we have joins, we must use a subquery for update
# because standard UPDATE does not support JOINs on many DBs.
pk_column = self.model._meta.db_pk_column
subquery = self._db.query_class.from_(table).select(table[pk_column])
subquery._wheres = self.query._wheres
subquery._havings = self.query._havings
subquery._joins = self.query._joins
if hasattr(self.query, "_limit"):
subquery._limit = self.query._limit
if hasattr(self.query, "_orderbys"):
subquery._orderbys = self.query._orderbys
Comment on lines +1315 to +1323
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there reason why you was able to cleanly make subquery based on query in delete, but wasn't able to do it in update?
Also - regarding "hasattr" calls - is there any case where there are no such fields on query? As far as I see - they are always set in init


# To avoid MySQL Error 1093, we wrap the subquery in another SELECT
# To avoid MySQL Error 1235, the outer SELECT shouldn't have LIMIT
wrapper = self._db.query_class.from_(subquery.as_("_t")).select(Table("_t")[pk_column])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use this hack only where it is neccessary, you can do somthing like this:

  if self.capabilities.dialect == "mysql":
      # MySQL Error 1093: wrap in an extra SELECT to decouple target table reference
      final = self._db.query_class.from_(subquery.as_("_t")).select(Table("_t")[pk_column])
  else:
      # PostgreSQL, SQLite, etc. can use the subquery directly
      final = subquery


self.query = self._db.query_class.update(table)
self.query = self.query.where(table[pk_column].isin(wrapper))

for key, value in self.update_kwargs.items():
field_object = self.model._meta.fields_map.get(key)
if not field_object:
Expand Down Expand Up @@ -1391,6 +1411,21 @@ def _make_query(self) -> None:
annotations=self._annotations,
)
self.resolve_filters()
if self._joined_tables:
# If we have joins, we must use a subquery for deletion
# because standard DELETE FROM does not support JOINs.
pk_column = self.model._meta.db_pk_column
subquery = self.query.select(self.model._meta.basetable[pk_column])

# To avoid MySQL Error 1093, we wrap the subquery in another SELECT
# To avoid MySQL Error 1235, the outer SELECT shouldn't have LIMIT
# We use the connection's query class directly to avoid carrying over
# the base table into the FROM clause.
wrapper = self._db.query_class.from_(subquery.as_("_t")).select(Table("_t")[pk_column])

self.query = copy(self.model._meta.basequery)
self.query = self.query.where(self.model._meta.basetable[pk_column].isin(wrapper))

self.query._delete_from = True
return

Expand Down