我正在使用sqlalchemy的ORM創建映射到SQL表的類。我遇到了在這些類之間產生關係的問題,因爲它們在聲明類之前互相引用。當我運行代碼解釋器投訴NameError: name 'Account' is not defined
Python - 類在引用前引用另一個類
我已經在下面包含了一個代碼示例,演示了我如何聲明這些類。
class Location(Base):
__tablename__ = 'locations'
id = Column(Integer, primary_key=True)
name = Column(String)
address = Column(String)
city = Column(String)
state = Column(String)
zip_code = Column(String)
account = sa.orm.relationship('Account', order_by=Account.id, back_populates='location')
entity = sa.orm.relationship('Entity', order_by=Entity.id, back_populates='location')
def __repr__(self):
return "<Location(name='{}', address='{}', city='{}', state='{}', zip_code='{}')>".\
format(self.name, self.address, self.city, self.state, self.zip_code)
class Account(Base):
__tablename__ = 'accounts'
id = Column(Integer, primary_key=True)
name = Column(String)
number = Column(String)
institution = Column(String)
# entity_id = Column(Integer, sa.ForeignKey('entities.id'))
entity = sa.orm.relationship('Entity', back_populates='accounts')
location = sa.orm.relationship('Location', order_by=Location.id, back_populates='account')
def __repr__(self):
return "<Account(name='{}', account={}, institution={}, entity={})>".\
format(self.name, self.number, self.institution, self.entity)
class Entity(Base):
__tablename__ = 'entities'
id = Column(Integer, primary_key=True)
name = Column(String)
accounts = sa.orm.relationship('Account', order_by=Account.id, back_populates='entity')
location = sa.orm.relationship('Location', order_by=Location.id, back_populates='entity')
def __repr__(self):
return "<Entity(name='{}', location='{}')>".format(self.name, self.location)
我在這裏錯過了什麼?有沒有辦法定義所有的類,然後儘可能地用函數調用它們?例如與功能,它所有的功能後,在底部打電話主要是簡單的定義:
def main():
foo()
def foo():
if __name__=='__main__':
main()
謝謝!在發佈這個問題後,我意識到這個問題可能與orm關係中的order_by參數有關,但我沒有意識到還有另一種方法來定義這些問題以避免此問題。 – Evan