如何从SQLAlchemy关系设置默认值?

时间:2013-12-09 18:30:43

标签: python sqlalchemy

我有一组像这样定义的类:

class CrosstabCohort(Base):
    __table__ = crosstab_cohorts
    # has property is_column
    cohort = relationship(Cohort, innerjoin=True)

class Crosstab(Base):
    __table__ = crosstabs

    columns = relationship(CrosstabCohort,
            #secondary=crosstab_cohorts,
            primaryjoin=and_(
                crosstabs.c.crosstab_id == crosstab_cohorts.c.crosstab_id,
                crosstab_cohorts.c.is_column == True),
            order_by=crosstab_cohorts.c.created_at,
            )

    rows = relationship(CrosstabCohort,
            #secondary=crosstab_cohorts,
            primaryjoin=and_(
                crosstabs.c.crosstab_id == crosstab_cohorts.c.crosstab_id,
                crosstab_cohorts.c.is_column == False),
            order_by=crosstab_cohorts.c.created_at,
            )

当我通过调用关系实例上的append添加列或行对象时,我希望is_column属性自动设置为True或{{1}取决于我追加它的关系。这可能吗?目前,当我附加到这些关系并尝试提交时,我从数据库收到一个错误,即未设置False属性。这可以自动完成,还是必须在每个is_column对象上设置is_column属性?

0 个答案:

没有答案