SQLAlchemy模型中的循环依赖

0 投票
1 回答
3029 浏览
提问于 2025-04-17 19:52

我正在创建一个网页应用,使用SQLAlchemy来访问数据库。现在我遇到了两个模型,它们互相引用,导致出现循环导入的问题,这样就抛出了以下异常:

--- SNIPP --- (output modified to hide software name)
  File "/opt/promethium/service/opt/xxxx/lib/python/api/server/server.py", line 11, in <module>
    from api.server.controllers import *
  File "/opt/promethium/service/opt/xxxx/lib/python/api/server/controllers/__init__.py", line 3, in <module>
    from messagescontroller import MessagesController
  File "/opt/promethium/service/opt/xxxx/lib/python/api/server/controllers/messagescontroller.py", line 7, in <module>
    from api.server.models import MessageModel, EmailModel, KeyModel, MessagerecipientModel
  File "/opt/promethium/service/opt/xxxx/lib/python/api/server/models/__init__.py", line 6, in <module>
    from keymodel import KeyModel
  File "/opt/promethium/service/opt/xxxx/lib/python/api/server/models/keymodel.py", line 18, in <module>
    from api.server.models import ApplicationModel, EmailModel
ImportError: cannot import name EmailModel

这些是相关的文件和它们的代码:

__init__.py

from applicationmodel import ApplicationModel
# some other imports here
from emailmodel import EmailModel
from keymodel import KeyModel
# some more imports here

keymodel.py

from sqlalchemy import Column, Integer, String, Date
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref, deferred
from sqlalchemy.ext.declarative import _declarative_constructor 
from sqlalchemy import event

from pyncacoreapi.server.models import ApplicationModel, EmailModel
from pyncacoreapi.server.libs.store import Base

class KeyModel(ApplicationModel, Base):
    __tablename__ = "keys"

    id = Column(Integer, primary_key=True)
    email_id = Column(Integer, ForeignKey("emails.id"))
    email = relationship("EmailModel", uselist=False, backref=backref("keys"))
    symmetrickey = deferred(Column(String,unique=True))
    certificate = deferred(Column(String,unique=True))
    privatekey = deferred(Column(String,unique=True))
    publickey = deferred(Column(String,unique=True))
    keymaterial = Column(String,unique=False)

emailmodel.py

from sqlalchemy import Column, Integer, String, Date
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.declarative import _declarative_constructor 

from pyncacoreapi.server.models import ApplicationModel, KeyModel, DomainModel

from pyncacoreapi.server.libs.store import Base, Db

from sqlalchemy import event

class EmailModel(ApplicationModel, Base):
    __tablename__ = "emails"

    id = Column(Integer, primary_key=True)
    email = Column(String, unique=True)
    domain_id = Column(Integer, ForeignKey("domains.id"))
    domain = relationship("DomainModel",backref=backref("emails", order_by=id))
    account_id = Column(Integer, ForeignKey("accounts.id"))
    account = relationship("AccountModel",backref=backref("emails", order_by=id))

我该怎么做才能正确处理这些导入的事情呢?有什么最佳实践或者“黄金法则”吗?感谢任何帮助!!

1 个回答

0

在这个特定的情况下,根本不需要导入 EmailModel。你只需要修改 keymodel.py,把导入的部分去掉:

from pyncacoreapi.server.models import ApplicationModel

SQLAlchemy 会把用字符串表示的引用推迟到最后一刻再去解析,所以在这里 EmailModel 是否已经定义其实并不重要。你是在通过字符串在 email 关系中引用它。

撰写回答