阅读背景:

如何在SQLAlchemy中聚合连接表中的不同值?

来源:互联网 

Here is the schema:

这是架构:

post_tag = Table("post_tag", Base.metadata,
                 Column("post_id", Integer, ForeignKey("post.id")),
                 Column("tag_id ", Integer, ForeignKey("tag.id")))


class Post(Base):
    id = Column(Integer, primary_key=True)
    tags = relationship("Tag", secondary=post_tag, backref="post", cascade="all")
    collection_id = Column(Integer, ForeignKey("collection.id"))


class Tag(Base):
    id = Column(Integer, primary_key=True)
    description = Column("description", UnicodeText, nullable=False, default="")
    post_id = Column(Integer, ForeignKey("post.id"))


class Collection(Base):
    id = Column(Integer, primary_key=True)
    title = Column(Unicode(128), nullable=False) 
    posts = relationship("Post", backref="collection", cascade="all,delete-orphan")

    tags = column_property(select([Tag])
                           .where(and_(Post.collection_id == id, Tag.post_id == Post.id))
                           .correlate_except(Tag))
post_tag = Table(



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: