Python SQLAlchemy:检查两列之一的任何约束都不为null?
这可能是一个非常愚蠢的问题,但我在我的模型中有这样一个要求,Python SQLAlchemy:检查两列之一的任何约束都不为null?,python,postgresql,sqlalchemy,flask-sqlalchemy,Python,Postgresql,Sqlalchemy,Flask Sqlalchemy,这可能是一个非常愚蠢的问题,但我在我的模型中有这样一个要求,category或parent\u category至少是notnull 我的模型看起来像 class BudgetCategories(db.Model): __tablename__ = 'budget_categories' uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, unique=Tru
category
或parent\u category
至少是notnull
我的模型看起来像
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
作为后端数据库我对PostgreSQL
语法没有100%的把握,但以下添加到您的BudgetCategories
模型中应该使用:
我希望还不算太晚,但这应该可以做到,并且检查它是否与PostGreSQL DB一起工作:
class BudgetCategories(Base):
__tablename__ = 'budget_categories'
__table_args__ = (
CheckConstraint('coalesce(category , parent_category ) is not null'),
)
# ...
在我的SQLalchemy模型中,我需要XOR行为。我提出了以下定义(后端使用:PostgreSQL): -仅uuid-正常
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner, notebook_path) VALUES ('{"kind":"hourly"}',true,'akos', '/a/b/c/d/e.ipynb');
INSERT 0 1
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner, uuid) VALUES ('{"kind":"hourly"}',true,'akos', '7792bd5f-5819-45bf-8902-8cf43102434d');
INSERT 0 1
-uuid和笔记本电脑路径-按需失败
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner, uuid, notebook_path) VALUES ('{"kind":"hourly"}',true,'akos', '7792bd5f-5819-45bf-8902-8cf43102434f', '/a/b/c/d');
ERROR: new row for relation "scheduled_notebooks" violates check constraint "uuid_xor_notebook_path"
DETAIL: Failing row contains (567, /a/b/c/d, {"kind": "hourly"}, t, akos, null, null, null, 7792bd5f-5819-45bf-8902-8cf43102434f).
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner) VALUES ('{"kind":"hourly"}',true,'akos');
ERROR: new row for relation "scheduled_notebooks" violates check constraint "uuid_xor_notebook_path"
DETAIL: Failing row contains (568, null, {"kind": "hourly"}, t, akos, null, null, null, null).
-uuid和笔记本路径均未按要求出现故障
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner, uuid, notebook_path) VALUES ('{"kind":"hourly"}',true,'akos', '7792bd5f-5819-45bf-8902-8cf43102434f', '/a/b/c/d');
ERROR: new row for relation "scheduled_notebooks" violates check constraint "uuid_xor_notebook_path"
DETAIL: Failing row contains (567, /a/b/c/d, {"kind": "hourly"}, t, akos, null, null, null, 7792bd5f-5819-45bf-8902-8cf43102434f).
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner) VALUES ('{"kind":"hourly"}',true,'akos');
ERROR: new row for relation "scheduled_notebooks" violates check constraint "uuid_xor_notebook_path"
DETAIL: Failing row contains (568, null, {"kind": "hourly"}, t, akos, null, null, null, null).
至少有一个
类别
和父类
为非空
,或者至少有一个非空
。如果两者都已填充->良好,但至少类别或父类别不为空。我希望category不为null或parent_category不为nullCHECK constraint可以在数据库级别执行此操作,但不确定Python级别。也许您可以帮助我。如果您有两个以上的字段,并且其中只有一个字段应该有值,而其余字段应该为空,那么该怎么办?Thanks@ChickenFeet请大家解释一下为什么投票失败了?是因为我不知道的一些性能影响吗?也许是DB后端支持?我真的很好奇,因为我个人觉得这个解决方案很好。我很想知道@radzak。也许是因为阅读起来不那么明显?很有争议。我不能说它有毛病。
datalake=# INSERT INTO scheduled_notebooks (schedule,enabled,owner) VALUES ('{"kind":"hourly"}',true,'akos');
ERROR: new row for relation "scheduled_notebooks" violates check constraint "uuid_xor_notebook_path"
DETAIL: Failing row contains (568, null, {"kind": "hourly"}, t, akos, null, null, null, null).