sqlalchemy模型中的循环依赖

时间:2013-03-21 12:10:56

标签: python model sqlalchemy circular-dependency python-import

我正在创建一个使用SQLAlchemy访问数据库的webapp。我遇到了两个相互引用的模型,最终导致循环导入,引发了以下异常:

--- 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

这些是文件及其代码:

__初始化__。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 :(得分:0)

在这种特定情况下,根本不需要导入EmailModel。更改keymodel.py以删除导入:

from pyncacoreapi.server.models import ApplicationModel

SQLAlchemy推迟解析以字符串形式提供的引用,直到最后可能的时刻为止,因此在此处定义EmailModel无关紧要。您通过字符串在email关系中引用它。