Revisions
-
anddam revised this gist
Nov 25, 2014 . 1 changed file with 3 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,4 @@ In [29]: my_sub = db.session.query(TestModel.name, func.count(TestModel.id).label('count')).group_by(TestModel.name).subquery() In [30]: db.session.query(TestModel, my_sub.c.count).outerjoin(my_sub, TestModel.name==my_sub.c.name).all() Out[30]: [(<1: Bob>, 1), (<2: Alice>, 2), (<3: Alice>, 2)] -
anddam revised this gist
Nov 25, 2014 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ # ACTUAL RESULT (flaskenv)me@b ~> ipython test.py (<3: Alice>, 2) (<1: Bob>, 1) # DESIRED RESULT (flaskenv)me@b ~> ipython test.py (<3: Alice>, 2) (<2: Alice>, 2) (<1: Bob>, 1) -
anddam revised this gist
Nov 25, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,6 +32,6 @@ def hello_world(): db.session.add_all([bob, alice, alice_two]) db.session.commit() u = db.session.query(TestModel, func.count(TestModel.id)).group_by(TestModel.name).all() for x in u: print x -
anddam renamed this gist
Nov 25, 2014 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ from sqlalchemy import func app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///test.db' db = SQLAlchemy(app) @@ -23,12 +23,15 @@ def hello_world(): if __name__ == '__main__': with app.app_context(): db.create_all() if len(db.session.query(TestModel).all())==0: # insert some data, one Bob and two Alices bob = TestModel(name="Bob") alice = TestModel(name="Alice") alice_two = TestModel(name="Alice") db.session.add_all([bob, alice, alice_two]) db.session.commit() u = db.session.query(TestModel.id, func.count(TestModel.id)).group_by(TestModel.name).all() for x in u: print x -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy import func app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite://' db = SQLAlchemy(app) class TestModel(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String()) def __repr__(self): return "<{}: {}>".format(self.id, self.name) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': with app.app_context(): db.create_all() bob = TestModel(name="Bob") alice = TestModel(name="Alice") alice_two = TestModel(name="Alice") db.session.add_all([bob, alice, alice_two]) db.session.commit() u = db.session.query(TestModel, func.count(TestModel.id)).group_by(TestModel.name).all() for x in u: print x