Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlalchemy: UPDATE... LIMIT 1, not possible?

Tags:

sqlalchemy

In MySQL it is possible to limit the number of records affected by an update query. In an ideal world this should not be necessary, but having such a limit does in some cases help save your bacon :)

I'd have thought that in SQLAlchemy it can be achieved by something like:

tgt_meta.tables['ps_product'].update(tgt_meta.tables['ps_product'].c.id_product == product_id).values(**upd_product_values).limit(1)

But apparently this is not so.

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

Is there something else that I can try?

like image 718
wshyang Avatar asked Jul 04 '26 11:07

wshyang


1 Answers

The Mysql dialect has this thrown in as update(..., mysql_limit=x)

https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql-sql-extensions

like image 81
zzzeek Avatar answered Jul 06 '26 12:07

zzzeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!