Skip to content

Commit e247d17

Browse files
committed
fix: Skip auto index creation for async connections in sync context
1 parent 3835098 commit e247d17

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mongoengine/document.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ def _get_collection(cls):
245245
# Ensure indexes on the collection unless auto_create_index was
246246
# set to False. Plus, there is no need to ensure indexes on slave.
247247
db = cls._get_db()
248-
if cls._meta.get("auto_create_index", True) and db.client.is_primary:
249-
cls.ensure_indexes()
248+
if cls._meta.get("auto_create_index", True):
249+
# 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()
250256

251257
return cls._collection
252258

0 commit comments

Comments
 (0)