nivya wrote:
>
> I'm trying to run the following sql using sqlalchemy ORM -
>
> "delete from feed_items where feed_id=27 order by published_on asc
> limit 10;"
>
> I tried -
>
> session.query(FeedItem).filter_by(feed_id=27).order_by
> (FeedItem.published_on.asc()).limit(10).delete()
>
> But this is deleting all rows instead of limiting the deletion to 10
> rows. This was unexpected.
you can't use LIMIT with delete(). delete() only works with WHERE
criterion. I'm a little surprised its not checking for that so I've
added ticket #1487.
your best bet on this is to say
q =
session.query(FeedItem.id).filter_by(feed_id=27).order_by...).limit(10).subquery()
session.query(FeedItem).filter(FeedItem.id.in_(q)).delete()
> connection.execute(feed_items_table.delete().where
> (feed_items_table.c.feed_id == 27).order_by
> (feed_items_table.c.published_on.asc()).limit(10))
>
> This throws an error saying order_by is not an attribute of Delete.
same answer applies.
>
> Is engine.execute(""delete from feed_items where feed_id=27 order by
> published_on asc limit 10") the only option?
this is a MySQL-only syntax that is a syntactical replacement for the
subquery approach outlined above and it would require strings. If you're
ambitious you can try making your own enhanced delete() construct by
subclassing Delete() and using ext.compiler, described at
http://www.sqlalchemy.org/docs/05/reference/ext/compiler.html .
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---