0

I am using Flask and SQLAlchemy to connect and use an MS SQL Server database following this post: How to build a flask application around an already existing database?

I have succeeded in creating Models using the declarative base using Flask-SQLAlchemy:

db = SQLAlchemy(app)
db.Model.metadata.reflect(bind=db.engine)

class Orders(db.Model):
    __table__ = db.Model.metadata.tables['orders']

    def __repr__(self):
        return '<Order %r>' % self.OrderID

models.Orders().query.all()   // returns [<Order 240579>, <Order 240580>]

result = db.session.execute(  // returns the same two rows
   'Select top 3 [OrderID] FROM [orders]'
) 

However, the problem is it is returning just two rows (with OrderIDs 240579 and 240580) even though the table contains lots and lots of orders.

These orders are from the middle of the table (i.e., there are OrderIDs that take on smaller values than 240579), although they are consecutive rows.

As shown above even when I execute a raw SQL query telling it to return the top three, only the same two are returned.

I am new to SQLAlchemy - how should I begin to debug this issue?

Matt-Heun Hong
  • 408
  • 3
  • 13
  • 1
    check this post may help you. [automatically-reflect-database-to-sqlalchemy](http://stackoverflow.com/questions/6290162/how-to-automatically-reflect-database-to-sqlalchemy-declarative#6307733) – metmirr Dec 08 '16 at 18:52
  • All the answers in that post incorporate the line metadata.reflect(bind=db.engine) or some variation of it. I think this line itself is incompletely reflecting the database – Matt-Heun Hong Dec 09 '16 at 16:01

0 Answers0