Giản, tôi có cấu trúc sau lớp (trong một tập tin duy nhất):SQLAlchemy không thể tìm thấy một tên lớp
Base = declarative_base()
class Item(Base):
__tablename__ = 'item'
id = Column(BigInteger, primary_key=True)
# ... skip other attrs ...
class Auction(Base):
__tablename__ = 'auction'
id = Column(BigInteger, primary_key=True)
# ... skipped ...
item_id = Column('item', BigInteger, ForeignKey('item.id'))
item = relationship('Item', backref='auctions')
tôi nhận được lỗi sau từ này:
sqlalchemy.exc.InvalidRequestError
InvalidRequestError: When initializing mapper Mapper|Auction|auction, expression
'Item' failed to locate a name ("name 'Item' is not defined"). If this is a
class name, consider adding this relationship() to the Auction class after
both dependent classes have been defined.
Tôi không chắc chắn làm thế nào Python không thể tìm thấy các hạng mục, như ngay cả khi đi qua lớp, chứ không phải là tên như một chuỗi, tôi nhận được cùng một lỗi. Tôi đã đấu tranh để tìm các ví dụ về làm thế nào để làm các mối quan hệ đơn giản với SQLAlchemy vì vậy nếu có điều gì đó khá rõ ràng sai ở đây tôi xin lỗi.
Câu trả lời này cũng hữu ích: http://stackoverflow.com/questions/7478403/sqlalchemy-classes-across-files –
Câu trả lời này cũng hữu ích: http://stackoverflow.com/a/4235691/232794 – Sardathrion