【发布时间】:2014-01-23 11:47:51
【问题描述】:
这可能是完全愚蠢的问题,但我的模型中有这样的要求,至少category 或parent_category 是not null
我的模型看起来像
class BudgetCategories(db.Model):
__tablename__ = 'budget_categories'
uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
unique=True)
budget_id = Column(GUID(), ForeignKey('budgets.uuid'), nullable=False)
budget = relationship('Budget', backref='budgetCategories')
category = Column('category', sa.types.String, nullable=True)
parent_category = Column('parent_category', sa.types.String, nullable=True)
amount = Column('amount', Numeric(10, 2), nullable=False)
recurring = Column('recurring', sa.types.Boolean,
nullable=False)
created_on = Column('created_on', sa.types.DateTime(timezone=True),
nullable=False)
我该如何指定。我什至不知道该尝试什么
欢迎指点
我使用PostgreSQL作为后端数据库
【问题讨论】:
-
category和parent_category中至少有一个是not null或恰好其中一个是not null? -
至少。如果两者都已填充-> 好,但至少 category 或 parent_category 不为空。我希望澄清
-
a
category is not null or parent_category is not nullCHECK 约束会在数据库级别执行此操作,但不确定 Python 级别。
标签: python postgresql sqlalchemy flask-sqlalchemy