We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3835098 commit e247d17Copy full SHA for e247d17
mongoengine/document.py
@@ -245,8 +245,14 @@ def _get_collection(cls):
245
# Ensure indexes on the collection unless auto_create_index was
246
# set to False. Plus, there is no need to ensure indexes on slave.
247
db = cls._get_db()
248
- if cls._meta.get("auto_create_index", True) and db.client.is_primary:
249
- cls.ensure_indexes()
+ if cls._meta.get("auto_create_index", True):
+ # Skip index creation for async connections in sync context
250
+ # They should use async_ensure_indexes() instead
251
+ from mongoengine.connection import is_async_connection
252
+
253
+ if not is_async_connection(cls._meta.get("db_alias")):
254
+ if db.client.is_primary:
255
+ cls.ensure_indexes()
256
257
return cls._collection
258
0 commit comments