Mongoengine 连接失败:您尚未定义默认连接

Mongoengine Connection Failure: You have not defined a default connection

启动一个 python 项目,将 fastapi 与 mongoengine 结合使用。昨天还正常,今天运行任意一个有数据库连接的方法,出现连接失败错误(mongoengine.connection.ConnectionFailure:你没有定义默认连接)。

我的联系人:

from mongoengine import connect

connect(db="mydb", host='localhost', port=27017, alias='default')

我的模特:

from mongoengine.document import Document
from mongoengine.fields import StringField, BooleanField, ListField, ReferenceField
from mongoengine import PULL


class Permissao(Document):
    permissao = StringField()


class Grupo(Document):
    grupo = StringField()
    
    permissoes = ListField(ReferenceField('Permissao', reverse_delete_rule=PULL))


class Usuario(Document):
    nome_usuario = StringField()
    ativo = BooleanField()
    senha = StringField()
    
    grupos = ListField(ReferenceField('Grupo', reverse_delete_rule=PULL))
    permissoes = ListField(ReferenceField('Permissao', reverse_delete_rule=PULL))

成功了,我将连接切换到模型并放置了一个元字段,请在下面查看:

from mongoengine.document import Document
from mongoengine.fields import StringField, BooleanField, ListField, ReferenceField
from mongoengine import PULL
from mongoengine import connect

connect(db="mydb", host="localhost", port=27017, alias="mydb-alias")

class Permissao(Document):
    permissao = StringField()

    meta = {"db_alias" : "mydb-alias" , "collection" : "permissao"}


class Grupo(Document):
    grupo = StringField()
    
    permissoes = ListField(ReferenceField('Permissao', reverse_delete_rule=PULL))
    
    meta = {"db_alias" : "mydb-alias" , "collection" : "grupo"}


class Usuario(Document):
    nome_usuario = StringField()
    ativo = BooleanField()
    senha = StringField()
    
    grupos = ListField(ReferenceField('Grupo', reverse_delete_rule=PULL))
    permissoes = ListField(ReferenceField('Permissao', reverse_delete_rule=PULL))

    meta = {"db_alias" : "mydb-alias" , "collection" : "usuario"}