flask-sqlalchemy

Flask SQLAlchemy query, specify column names

故事扮演 提交于 2019-11-26 12:53:21
How do I specify the column that I want in my query using a model (it selects all columns by default)? I know how to do this with the sqlalchmey session: session.query(self.col1) , but how do I do it with with models? I can't do SomeModel.query() . Is there a way? David McKeone You can use the with_entities() method to restrict which columns you'd like to return in the result. ( documentation ) result = SomeModel.query.with_entities(SomeModel.col1, SomeModel.col2) Depending on your requirements, you may also find deferreds useful. They allow you to return the full object but restrict the

SQLAlchemy ManyToMany secondary table with additional fields

a 夏天 提交于 2019-11-26 11:55:59
问题 I have 3 tables: User, Community, community_members (for relationship many2many of users and community). I create this tables using Flask-SQLAlchemy: community_members = db.Table(\'community_members\', db.Column(\'user_id\', db.Integer, db.ForeignKey(\'user.id\')), db.Column(\'community_id\', db.Integer, db.ForeignKey(\'community.id\')), ) class Community(db.Model): __tablename__ = \'community\' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False,

How to execute raw SQL in Flask-SQLAlchemy app

三世轮回 提交于 2019-11-26 11:30:45
How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I've tried: connection = db.session.connection() connection.execute( <sql here> ) But I keep getting gateway errors. Miguel Have you tried: result = db.engine.execute("<sql here>") or: from sqlalchemy import text sql = text('select name from penguins') result = db.engine.execute(sql) names = [row[0] for row in result] print names SQL Alchemy session objects have

How to update SQLAlchemy row entry?

余生颓废 提交于 2019-11-26 10:07:52
问题 Assume table has three columns: username , password and no_of_logins . When user tries to login, it\'s checked for an entry with a query like user = User.query.filter_by(username=form.username.data).first() If password matches, he proceeds further. What I would like to do is count how many times the user logged in. Thus whenever he successfully logs in, I would like to increment the no_of_logins field and store it back to the user table. I\'m not sure how to run update query with SqlAlchemy.

Querying with function on Flask-SQLAlchemy model gives BaseQuery object is not callable error

微笑、不失礼 提交于 2019-11-26 09:56:44
问题 I want to query services between two dates and sum their prices. When I try to use func.sum with Services.query , I get TypeError: BaseQuery object is not callable . How do I query using a function with Flask-SQLAlchemy? Services.query(func.sum(Services.price)).filter(Services.dateAdd.between(start, end)) 回答1: Model.query is a shortcut to db.session.query(Model) , it's not callable. If you're not querying a model, continue to use db.session.query(...) as you would with regular SQLAlchemy. db

SQLAlchemy ORM conversion to pandas DataFrame

旧街凉风 提交于 2019-11-26 08:44:25
问题 This topic hasn\'t been addressed in a while, here or elsewhere. Is there a solution converting a SQLAlchemy <Query object> to a pandas DataFrame? Pandas has the capability to use pandas.read_sql but this requires use of raw SQL. I have two reasons for wanting to avoid it: 1) I already have everything using the ORM (a good reason in and of itself) and 2) I\'m using python lists as part of the query (eg: .db.session.query(Item).filter(Item.symbol.in_(add_symbols) where Item is my model class

Case Insensitive Flask-SQLAlchemy Query

こ雲淡風輕ζ 提交于 2019-11-26 08:09:05
问题 I\'m using Flask-SQLAlchemy to query from a database of users; however, while user = models.User.query.filter_by(username=\"ganye\").first() will return <User u\'ganye\'> doing user = models.User.query.filter_by(username=\"GANYE\").first() returns None I\'m wondering if there\'s a way to query the database in a case insensitive way, so that the second example will still return <User u\'ganye\'> 回答1: You can do it by using either the lower or upper functions in your filter: from sqlalchemy

Flask SQLAlchemy query, specify column names

人盡茶涼 提交于 2019-11-26 03:35:37
问题 How do I specify the column that I want in my query using a model (it selects all columns by default)? I know how to do this with the sqlalchmey session: session.query(self.col1) , but how do I do it with with models? I can\'t do SomeModel.query() . Is there a way? 回答1: You can use the with_entities() method to restrict which columns you'd like to return in the result. (documentation) result = SomeModel.query.with_entities(SomeModel.col1, SomeModel.col2) Depending on your requirements, you

How to execute raw SQL in Flask-SQLAlchemy app

寵の児 提交于 2019-11-26 03:27:27
问题 How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I\'ve tried: connection = db.session.connection() connection.execute( <sql here> ) But I keep getting gateway errors. 回答1: Have you tried: result = db.engine.execute("<sql here>") or: from sqlalchemy import text sql = text('select name from penguins') result = db

jsonify a SQLAlchemy result set in Flask [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-26 01:05:39
问题 This question already has an answer here: How to serialize SqlAlchemy result to JSON? 21 answers I\'m trying to jsonify a SQLAlchemy result set in Flask/Python. The Flask mailing list suggested the following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-collection-result/#04a0754b63387f87e59dda564bde426e : return jsonify(json_list = qryresult) However I\'m getting the following error back: TypeError: <flaskext.sqlalchemy.BaseQuery object at 0x102c2df90> is