SQLAlchemy, how to filter by empty array column?
Question:
I have that kind of Model in SQLAlchemy:
class MyModel(Base):
__tablename__ = 'my_models'
name = Column(String(255), nullable=False)
company_ids = Column(ARRAY(Integer), nullable=True)
I have intentionally created models with empty arrays and try to query them like this:
db.query(MyModel).filter(~MyModel.company_ids.any())
Which doesn’t return anything.
Is it possible to filter models that have empty array field?
============UPDATE===============
I was able to query it by using this method:
db.query(MyModel).filter(MyModel.company_ids == '{}')
However I think it is only PostgreSQL specific answer
Answers:
I was able to query it by using this method:
db.query(MyModel).filter(MyModel.company_ids == '{}')
However I think it is only PostgreSQL specific answer
I think SQLAlchemy’s way of doing this is
db.query(MyModel).filter(
func.cardinality(MyModel.company_ids == 0)
)
I have that kind of Model in SQLAlchemy:
class MyModel(Base):
__tablename__ = 'my_models'
name = Column(String(255), nullable=False)
company_ids = Column(ARRAY(Integer), nullable=True)
I have intentionally created models with empty arrays and try to query them like this:
db.query(MyModel).filter(~MyModel.company_ids.any())
Which doesn’t return anything.
Is it possible to filter models that have empty array field?
============UPDATE===============
I was able to query it by using this method:
db.query(MyModel).filter(MyModel.company_ids == '{}')
However I think it is only PostgreSQL specific answer
I was able to query it by using this method:
db.query(MyModel).filter(MyModel.company_ids == '{}')
However I think it is only PostgreSQL specific answer
I think SQLAlchemy’s way of doing this is
db.query(MyModel).filter(
func.cardinality(MyModel.company_ids == 0)
)