0

I can never seem to get the filter calls right when I want to check for null values.

Is this function unnecessary?

def sqla_false(field):
    if field.type.__class__ == db.String:
        return db.or_(field == None, field == '')
    return field == None

How does sqlalchemy store unset fields? What if one of my wtforms forms sends a "" as a value for one of the fields, would NULL or "" be stored in the database when I call form.populate_obj(obj)?

1 Answer 1

1

If you send '', then that is stored and read as the empty string. If you send None, then that is stored as NULL and read as None. Use isinstance(field, db.String) to check for type. Use db.and_(field.isnot(None), field != '') to check that it has a non-empty string value.

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

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.