SQLAlchemy:检查两列之一的任何约束都不为null?

day*_*mer 4 python postgresql sqlalchemy flask-sqlalchemy

这可能是完全愚蠢的问题,但我在我的模型中有这样的要求,至少要么是category或者parent_categorynot 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)
Run Code Online (Sandbox Code Playgroud)

我该如何指定.我甚至不知道该尝试什么

任何指针赞赏

PostgreSQL用作后端数据库

van*_*van 13

我不是100%确定PostgreSQL语法,但是在添加到BudgetCategories模型之后应该使用以下方法CheckConstraint:

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

    # @note: new
    __table_args__ = (
            CheckConstraint('NOT(category IS NULL AND parent_category IS NULL)'),
            )
Run Code Online (Sandbox Code Playgroud)


and*_*abs 8

我的 SQLalchemy 模型中需要XOR行为。我提出以下定义(使用后端:PostgreSQL):

from sqlalchemy.schema import (
    CheckConstraint
)

class ScheduledNotebook(Base):
    __table_args__ = (
        (CheckConstraint('(uuid::text IS NULL) <> (notebook_path IS NULL)', name='uuid_xor_notebook_path')),
    )

    id = Column(Integer, primary_key=True)
    notebook_path = Column(String, nullable=True, unique=True)
    uuid = Column(UUID(as_uuid=True), primary_key=True, unique=True, nullable=True)
Run Code Online (Sandbox Code Playgroud)

以及以下 Alembic 迁移(注意:自动生成不会检测到它 - 您必须手动添加它):

def upgrade():
    op.create_check_constraint(
        'uuid_xor_notebook_path',
        table_name='scheduled_notebooks',
        schema='metadata',
        condition='(uuid::text IS NULL) <> (notebook_path IS NULL)'
    )


def downgrade():
    op.drop_constraint('uuid_xor_notebook_path')
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力:

- 仅笔记本路径 - 确定

datalake=#  INSERT INTO scheduled_notebooks (schedule,enabled,owner, notebook_path) VALUES ('{"kind":"hourly"}',true,'akos', '/a/b/c/d/e.ipynb');
INSERT 0 1
Run Code Online (Sandbox Code Playgroud)

- 只有uuid - 好的

datalake=#  INSERT INTO scheduled_notebooks (schedule,enabled,owner, uuid) VALUES ('{"kind":"hourly"}',true,'akos', '7792bd5f-5819-45bf-8902-8cf43102434d');
INSERT 0 1
Run Code Online (Sandbox Code Playgroud)

- 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).
Run Code Online (Sandbox Code Playgroud)

- uuid 和笔记本路径都不是 - 根据需要失败

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).
Run Code Online (Sandbox Code Playgroud)