SQLAlchemy: Any constraint to check one of the two columns is not null?

ⅰ亾dé卋堺 提交于 2019-12-01 20:45:16

I am not 100% sure about the PostgreSQL syntax, but following addition to your BudgetCategories model should do the trick using CheckConstraint:

class BudgetCategories(Base):
    __tablename__ = 'budget_categories'
    # ...

    # @note: new
    __table_args__ = (
            CheckConstraint('NOT(category IS NULL AND parent_category IS NULL)'),
            )

I hope is not too late but this should do the trick and it is checked to work with a PostGreSQL DB:

class BudgetCategories(Base):
    __tablename__ = 'budget_categories'
    __table_args__ = (
        CheckConstraint('coalesce(category , parent_category ) is not null'),
    )
    # ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!