SQLAlchemy:如何在使用column_property时访问类属性?

时间:2017-03-30 16:07:17

标签: python sqlalchemy

我的数据库中有3个表\类:电影,图片和MediaFolders, 当我添加一个媒体文件夹时,python扫描给定的路径并将该路径中的图片和电影添加到数据库中。

我希望有能力告诉我在给定的MediaFolder中有多少图片\电影,所以我添加了一个< movie_count'的column_property。和' picture_count'
到我的班级。然而,这似乎不起作用,正如SQL输出所显示的那样,' path_to_dir'转换为“没有名称'

如果有人指出我做错了什么,我会很感激。

class MediaFolder(Base):
    __tablename__ = 'MediaFolder'

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)
    path_to_dir = Column(String, nullable=False, unique=True)
    is_picture = Column(Boolean)
    is_video = Column(Boolean)

    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())

    last_scan = Column(DateTime)

    movie_count = column_property(
        select([func.count(Movie.id)]).where(Movie.path_to_file.like('{}%'.format(path_to_dir)).correlate_except(Movie)
    ))

    picture_count = column_property(
        select([func.count(Picture.id)]).where(Picture.path_to_file.like('{}%'.format(path_to_dir))).correlate_except(
            Picture)
    )

SQL echo输出如下:

2017-03-30 18:53:49,671 INFO sqlalchemy.engine.base.Engine SELECT (SELECT count("Movie".id) AS count_1 
FROM "Movie" 
WHERE "Movie".path_to_file LIKE ?) AS anon_1, (SELECT count("Picture".id) AS count_2 
FROM "Picture" 
WHERE "Picture".path_to_file LIKE ?) AS anon_2, "MediaFolder".id AS "MediaFolder_id", "MediaFolder".name AS "MediaFolder_name", "MediaFolder".path_to_dir AS "MediaFolder_path_to_dir", "MediaFolder".is_picture AS "MediaFolder_is_picture", "MediaFolder".is_video AS "MediaFolder_is_video", "MediaFolder".created_at AS "MediaFolder_created_at", "MediaFolder".updated_at AS "MediaFolder_updated_at", "MediaFolder".last_scan AS "MediaFolder_last_scan" 
FROM "MediaFolder" 
WHERE "MediaFolder".id = ?
2017-03-30 18:53:49,672 INFO sqlalchemy.engine.base.Engine ('(no name)%', '(no name)%', 1)

修改

电影模特:

class Movie(Base):
        __tablename__ = 'Movie'

        id = Column(Integer, primary_key=True, index=True)
        path_to_file = Column(String)

图片模型:

class Picture(Base):
        __tablename__ = 'Picture'

        id = Column(Integer, primary_key=True, index=True)
        path_to_file = Column(String)

关于调用movie_count和picture_count的代码:它们都是column_property,这意味着只要加载模型就会加载它们,例如: a = Session.query(MediaFolder).filter(MediaFolder.id == 1).one()一个人应该能够a.movie_count并获得电影数量。 上面的SQL echo输出是我尝试以这种方式加载MediaFolder时得到的。

PS 我还有其他column_property不依赖like关键字,而且效果非常好。

例如,这是Actor模型中的列属性,它计算某个actor具有的图片数量:

 picture_count = column_property(
        select([func.count(picture_actors.c.actor_id)]).where(picture_actors.c.actor_id == id).correlate_except(
            picture_actors)
    )

其中picture_actors是关系表(而不是模型),定义如下:

picture_actors = Table('Picture_Actor', Base.metadata,
                       Column('picture_id', Integer, ForeignKey('Picture.id'), index=True),
                       Column('actor_id', Integer, ForeignKey('Actor.id'), index=True),
                       )

0 个答案:

没有答案