-1

I have a table called recentOrders and I want to return the row which has the highest value (basically the latest order number) from the column OrderID.

I can return a single seemingly random row with:

s = recentOrders.select()
rs = s.execute()
row = rs.fetchone() 
print(row)

and I've tried various things along the lines of:

s = recentOrders.select().order_by(OrderID.desc())
rs = s.execute()
row = rs.fetchone()
print(row)

But I get an error of

NameError: name 'OrderID' is not defined

What's the correct syntax for a query?

Johnny John Boy
  • 3,009
  • 5
  • 26
  • 50
  • Try `recentOrders.c.OrderID.desc()`, if `recentOrders` is a `Table`. That NameError is the result of trying to use a non existent variable name, so perhaps [refresh your Python basics](https://docs.python.org/3/tutorial/). – Ilja Everilä Jan 05 '18 at 11:36
  • Thank you, what does the c mean in the recentOrders.c.OrderID.desc() ? – Johnny John Boy Jan 05 '18 at 11:46
  • [It is an alias](http://docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.Table.c) of the [`columns` attribute](http://docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.Table.columns), which is a name-based collection of columns of the `Table`. I'd suggest you also read the [SQLAlchemy tutorials](http://docs.sqlalchemy.org/en/latest/core/tutorial.html). – Ilja Everilä Jan 05 '18 at 11:51

1 Answers1

0

Thanks to Ilja Everilä for the answer.

s = recentOrders.select().order_by(recentOrders.c.OrderID.desc())
rs = s.execute()
row = rs.fetchone()
Johnny John Boy
  • 3,009
  • 5
  • 26
  • 50