首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让“标签”影响关系(Column_property)

如何让“标签”影响关系(Column_property)
EN

Stack Overflow用户
提问于 2017-08-21 17:22:36
回答 1查看 137关注 0票数 0

我正在与Flask-SQLAlchemy合作一个项目。

模型看起来是这样的:汽车有组件,组件可能有问题

car有一个column_property 'needs_repair‘,当汽车的组件有问题时,这是真的

代码语言:javascript
复制
 needs_repair = column_property(exists().where(and_(
        carcomponent.columns['car_id'] == id,
        carcomponent.columns['component_id'] == componentissue.columns['component_id']
    )))

我添加了一个带有‘skip’列的标签表格,标签是通过表格issue_car_tag分配的(忽略组件,只引用特定的汽车问题关系)。

现在,如果所有指定的标签都有skip = False或者没有指定标签,我希望needs_repair为true

我如何扩展column_property来实现这一点?

编辑:模型/表格定义:

代码语言:javascript
复制
class Component(Base):
    id = db.Column(db.Integer, primary_key=True)
    [...]
    issues = db.relationship('ISsue', secondary=componentissue, lazy='dynamic',
        back_populates='components')

    cars = db.relationship('Car', lazy = 'dynamic', secondary=carcomponent,
        back_populates="component"

    broken = column_property(exists().where(componentissue.columns['component_id'] == id))

class Car(Base):
   id = db.Column(db.Integer, primary_key=True)
   [...] 
   components = db.relationship('Component', secondary=carcomponent,
        back_populates="cars", lazy='dynamic')
   needs_repair = column_property(exists().where(and_(
            carcomponent.columns['car_id'] == id,
            carcomponent.columns['component_id'] == componentissue.columns['component_id']
        )))

class Issue(Base):
    __tablename__ = "issues"
    [...]    
    components = db.relationship('Component' lazy = 'dynamic', secondary=componentissue,
        back_populates='issues')

class Tag(Base):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, unique=True)
    description = db.Column(db.Text, nullable=False, default="")
    skip = db.Column(db.Boolean, default = False)

class Issue_Car_Tag(Base):
    id = db.Column(db.Integer, primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
    car_id = db.Column(db.Integer, db.ForeignKey('car.id'))
    issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'))
    tag = db.relationship('Tag', backref=db.backref('issue_car_tags'))
    car = db.relationship('Car',  backref=db.backref('issue_car_tags'))
    issue = db.relationship('Issue',  backref=db.backref('issue_car_tags'))
EN

回答 1

Stack Overflow用户

发布于 2017-08-21 19:54:36

如果将Car的定义移到Tag和Issue_Car_Tag的定义之后,或者以其他方式引用这些表,则可以生成以下查询结构

代码语言:javascript
复制
func.coalesce(func.bool_and(not_(Tag.skip)), False).\
    select().\
    where(Tag.id == Issue_Car_Tag.tag_id).\
    where(Issue_Car_Tag.car_id == id).\
    as_scalar()

并在OR中将其与现有检查一起使用:

代码语言:javascript
复制
needs_repair = column_property(
    or_(func.coalesce(func.bool_and(not_(Tag.skip)), False).
            select().
            where(Tag.id == Issue_Car_Tag.tag_id).
            where(Issue_Car_Tag.car_id == id).
            as_scalar(),
        exists().where(and_(
            carcomponent.c.car_id == id,
            carcomponent.c.component_id == componentissue.c.component_id))))

查询使用关联表issue_car_tag选择与汽车相关的标签,并对跳过值进行aggregatescoalescing a null result或all null value。

请注意,如果没有分配标记,这将导致false,因此您必须单独处理。如果我理解正确的话,这个查询已经由EXISTS表达式处理了。换句话说,如果标签存在,并且所有标签都将skip设置为false,则新的查询结果为true。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45793519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档