Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if at least one of two specific fields in a table is NOT NULL in Sqlalchemy ORM? [duplicate]

I have two fields:

class Person(base):
     field1 = Column(String(32), unique=True, nullable=True)
     field2 = Column(String(128), unique=True, nullable=True)
     field3 = ...
     ...

I need to create a constraint, to check if at least one of [field1, field2] is available. I guess I need to use CheckConstraint, but I failed to get a result. Any ideas?

like image 657
Farshid Ashouri Avatar asked Oct 31 '25 17:10

Farshid Ashouri


1 Answers

Thanks to this post:

__table_args__ = (
        CheckConstraint('NOT(field1 IS NULL AND field2 IS NULL)'),
        )
like image 182
Farshid Ashouri Avatar answered Nov 03 '25 08:11

Farshid Ashouri