3

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

I think SQLAlchemy's way of doing this is

db.query(MyModel).filter(
 func.cardinality(MyModel.company_ids == 0)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.