Tôi đang sử dụng thừa kế bảng cụ thể với SQLAlchemy. Trong lớp mô hình kiểu suy giảm, tôi đã cấu hình nó thành công.Mối quan hệ đa hình SQLAlchemy với kế thừa bê tông
Mã của tôi chỉ thích:
class Entry(AbstractConcreteBase, db.Model):
"""Base Class of Entry."""
id = db.Column(db.Integer, primary_key=True, nullable=False)
created = db.Column(db.DateTime, nullable=False)
post_id = declared_attr(lambda c: db.Column(db.ForeignKey("post.id")))
post = declared_attr(lambda c: db.relationship("Post", lazy="joined"))
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
@declared_attr
def __mapper_args__(cls):
# configurate subclasses about concrete table inheritance
return {'polymorphic_identity': cls.__name__,
'concrete': True} if cls.__name__ != "Entry" else {}
class TextEntry(Entry):
"""Text and Article Entry."""
text = db.deferred(db.Column(db.Text, nullable=False))
class PhotoEntry(Entry):
"""Photo Entry."""
path = db.deferred(db.Column(db.String(256), nullable=False))
Nó hoạt động tốt khi thử nghiệm nó trong vỏ:
>>> from models.entry import Entry
>>>
>>> Entry.query.all()
[<PhotoEntry 'Title' created by tonyseek>,
<PhotoEntry 'TITLE 2' created by tonyseek>,
<PhotoEntry 'Title 3' created by tonyseek>,
<PhotoEntry 'Title 4' created by tonyseek>,
<TextEntry 'Title' created by tonyseek>]
Sau đó, tôi rơi vào rắc rối khi thiết lập các mối quan hệ trong các mô hình khác. Mỗi mục có khóa ngoài post_id
để tham gia Post
mô hình, nhưng tôi không thể xác định tham chiếu ngược trong Post
. Điều đó không thể làm việc:
class Post(db.Model):
"""An Post."""
id = db.Column(db.Integer, primary_key=True, nullable=False)
description = db.Column(db.Unicode(140), nullable=False)
entries = db.relationship(Entry, lazy="dynamic")
Nó nêu ra một ngoại lệ và nói:
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: Class 'models.entry.Entry' is not mapped.
Rõ ràng các Entry
là một lớp trừu tượng, mà không thể được ánh xạ tới một thực tồn tại bảng. Tài liệu trên trang web chính thức có một ví dụ nhưng lớp cơ sở của nó không trừu tượng. Bây giờ làm thế nào tôi nên làm gì để thiết lập mối quan hệ đa hình với một mô hình trừu tượng?