Lorence
Lorence

Reputation: 25

Flask SQLAlchemy filter using regex expression

I have this raw mysql query, SELECT user_id FROM user WHERE email_address NOT REGEXP '[^@]+@[^@]+\.[^@]+', this filters all email addresses having invalid email addresses.

I will need an SQLAlchemy equivalent for this.

I have tried this query, User.query.filter(User.email_address.op('regexp')(r'[^@]+@[^@]+\.[^@]+')).all(), but it filters all the valid email addresses. I just need to know how to add the NOT in this query.

Upvotes: 2

Views: 2373

Answers (1)

Yujiro
Yujiro

Reputation: 361

try this:

User.query.filter(User.email_address.op('not regexp')(r'[^@]+@[^@]+\.[^@]+')).all()

Upvotes: 2

Related Questions