Skip to content

Instantly share code, notes, and snippets.

@anddam
Forked from anonymous/untitled.py
Last active December 9, 2022 14:45

Revisions

  1. anddam revised this gist Nov 25, 2014. 1 changed file with 3 additions and 10 deletions.
    13 changes: 3 additions & 10 deletions results
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,4 @@
    # ACTUAL RESULT
    In [29]: my_sub = db.session.query(TestModel.name, func.count(TestModel.id).label('count')).group_by(TestModel.name).subquery()

    (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)
    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)]
  2. anddam revised this gist Nov 25, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions results
    Original 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)
  3. anddam revised this gist Nov 25, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.py
    Original 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.id, func.count(TestModel.id)).group_by(TestModel.name).all()
    u = db.session.query(TestModel, func.count(TestModel.id)).group_by(TestModel.name).all()
    for x in u:
    print x
  4. anddam renamed this gist Nov 25, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions untitled.py → test.py
    Original 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://'
    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()
    bob = TestModel(name="Bob")
    alice = TestModel(name="Alice")
    alice_two = TestModel(name="Alice")
    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, func.count(TestModel.id)).group_by(TestModel.name).all()
    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
  5. @invalid-email-address Anonymous created this gist Nov 24, 2014.
    34 changes: 34 additions & 0 deletions untitled.py
    Original 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