SQLAlchemy Model query-cheat-sheet Query Methods Here are some useful query methods to get to know. - Select records all() MyModel.query.all() Fetches all records from a table. Returns list of objects. first() MyModel.query.first() Fetches just the first result. Returns object or None. - Filtering filter_by() MyModel.query.filter_by(attr='some value') Similar to SELECT * from ... WHERE filter() Examples: MyModel.query.filter(MyOtherModel.some_attr='some value') OrderItem.query.filter(Product.id=3) Similar to filter_by , but instead, you specify attributes on a given Model.-- -- equals: query.filter(User.name == 'ed') -- not equals: query.filter(User.name != 'ed') LIKE : query.filter(User.name.like('%ed%')) ILIKE (case-insensitive LIKE): query.filter(User.name.ilike('%ed%')) IN: query.filter(User.name.in_(['ed', 'wendy', 'jack'])) NOT IN: query.filter(~User.name.in_(['ed', 'wendy', 'jack...
Comments
Post a Comment