2

I try to execute update query with limit in Flask, using Flask-SQLAlchemy (not SQLAlchemy!).

db.session.query(ItemObject).filter_by(owner_type=0,item_id=item_id.decode('hex')).update({'owner_type': '1'})

When i try ".limit(1)" or "update({'owner_type': '1'}, mysql_limit=1)" i geting error

AttributeError: 'long' object has no attribute 'limit'

or

TypeError: update() got an unexpected keyword argument 'mysql_limit'

How can i make this query without using execute()?

Hulii Borys
  • 272
  • 4
  • 15

1 Answers1

0

For your first error, it sounds like you're trying to call limit() after calling update().

db.session.query(ItemObject).filter_by(owner_type=0,item_id=item_id.decode('hex')).update({'owner_type': '1'})

This line returns a long because update() returns the number of rows that were matched (not modified!). You can't chain a limit after an update.

You also can't call limit on a query with update, so it wouldn't work anyways. That's just a MySQL thing.

Take a look at this previous answer - https://stackoverflow.com/a/25943713/175320 - which shows you how to get the same end result but in a portable manner using subqueries.

Community
  • 1
  • 1
TkTech
  • 4,729
  • 1
  • 24
  • 32
  • "That's just a MySQL thing." UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] https://dev.mysql.com/doc/refman/5.0/en/update.html – Hulii Borys May 04 '15 at 18:53