|
| 1 | +# Async Select Related Fix Documentation |
| 2 | + |
| 3 | +## Problem |
| 4 | +When using `select_related()` with async QuerySets in MongoEngine, a `TypeError: 'AsyncCursor' object is not an iterator` error occurs because the synchronous `select_related()` method tries to iterate over an `AsyncCursor` using synchronous iteration. |
| 5 | + |
| 6 | +## Solution |
| 7 | +We've implemented a new `async_select_related()` method that properly handles async cursors and dereferences. |
| 8 | + |
| 9 | +### Implementation Details |
| 10 | + |
| 11 | +1. **New Method**: `async_select_related(max_depth=1)` in `BaseQuerySet` |
| 12 | +2. **New Class**: `AsyncDeReference` in `mongoengine/dereference.py` that handles async operations |
| 13 | +3. **New Method**: `async_in_bulk(object_ids)` for efficient bulk fetching in async context |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | +Instead of: |
| 18 | +```python |
| 19 | +# This will fail with TypeError |
| 20 | +items = await self.skip(skip).limit(limit + 1).select_related().async_to_list() |
| 21 | +``` |
| 22 | + |
| 23 | +Use: |
| 24 | +```python |
| 25 | +# This works correctly |
| 26 | +items = await self.skip(skip).limit(limit + 1).async_select_related() |
| 27 | +``` |
| 28 | + |
| 29 | +### Example |
| 30 | + |
| 31 | +```python |
| 32 | +from mongoengine import Document, ReferenceField, StringField, connect_async |
| 33 | + |
| 34 | +class Author(Document): |
| 35 | + name = StringField(required=True) |
| 36 | + |
| 37 | +class Post(Document): |
| 38 | + title = StringField(required=True) |
| 39 | + author = ReferenceField(Author) |
| 40 | + |
| 41 | +class Comment(Document): |
| 42 | + content = StringField(required=True) |
| 43 | + post = ReferenceField(Post) |
| 44 | + author = ReferenceField(Author) |
| 45 | + |
| 46 | +# Connect to async MongoDB |
| 47 | +await connect_async('mydb') |
| 48 | + |
| 49 | +# Create data |
| 50 | +author = Author(name="John Doe") |
| 51 | +await author.async_save() |
| 52 | + |
| 53 | +post = Post(title="Hello World", author=author) |
| 54 | +await post.async_save() |
| 55 | + |
| 56 | +# Query with select_related |
| 57 | +posts = await Post.objects.async_select_related() |
| 58 | +# posts[0].author is now the actual Author document, not a reference |
| 59 | + |
| 60 | +# With nested references (max_depth=2) |
| 61 | +comments = await Comment.objects.async_select_related(max_depth=2) |
| 62 | +# comments[0].post and comments[0].author are dereferenced |
| 63 | +# Note: comments[0].post.author remains an AsyncReferenceProxy |
| 64 | +``` |
| 65 | + |
| 66 | +### Limitations |
| 67 | + |
| 68 | +1. **Nested References**: With `max_depth > 1`, deeply nested references may still be `AsyncReferenceProxy` objects that require explicit fetching: |
| 69 | + ```python |
| 70 | + # For deeply nested references |
| 71 | + author = await comment.post.author.fetch() |
| 72 | + ``` |
| 73 | + |
| 74 | +2. **Performance**: Like the sync version, `async_select_related()` performs additional queries to fetch all referenced documents. Use it when you know you'll need the referenced data. |
| 75 | + |
| 76 | +### Migration from Sync to Async |
| 77 | + |
| 78 | +If you're migrating from synchronous to asynchronous MongoEngine: |
| 79 | + |
| 80 | +1. Replace `select_related()` with `async_select_related()` |
| 81 | +2. Replace `in_bulk()` with `async_in_bulk()` if used directly |
| 82 | +3. Be aware that async operations return `AsyncReferenceProxy` for references by default |
| 83 | + |
| 84 | +### Testing |
| 85 | + |
| 86 | +The implementation includes comprehensive tests in `tests/test_async_select_related.py` covering: |
| 87 | +- Single reference dereferencing |
| 88 | +- Multiple reference fields |
| 89 | +- Filtering with select_related |
| 90 | +- Skip/limit with select_related |
| 91 | +- Empty querysets |
| 92 | +- Max depth handling |
0 commit comments