MasoniteFramework / orm

Masonite ORM is a beautiful Python ORM. It's also a nearly drop in replacement of the Orator ORM
https://orm.masoniteproject.com
MIT License
165 stars 48 forks source link

Is there some way to get the number of rows deleted by a query builder's `delete` method? #736

Closed jfly closed 2 years ago

jfly commented 2 years ago

Is your feature request related to a problem?

The query builder docs mention a delete method: https://orm.masoniteproject.com/query-builder#deletes, but I don't see any way of figuring out how many rows were affected by that delete.

What do we currently have to do now?

Right now, I'm turning around and immediately doing a SELECT ROW_COUNT() query, which may be a MySQL specific solution.

Describe the solution you'd like

It would be nice to have a standard way of fetching the number of rows affected by the delete. Perhaps delete() could return the count of deleted rows? I'm not sure if that would count as a breaking change or not. The docs don't seem to mention what delete returns, but the implementation clearly does return... something: https://github.com/MasoniteFramework/orm/blob/685a30409cd4b971c24c932d78b684a2be085f58/src/masoniteorm/query/QueryBuilder.py#L530-L565, although the docstring disagrees with the implementation: the docstring says it returns self, when it actually returns the result of self.new_connection().query...

Describe alternatives you've considered

No response

Would this be a breaking change ?

Anything else ?

No response

girardinsamuel commented 2 years ago

I will let @josephmancuso answer this one.

In the meantime, could you move this issue to Masonite ORM repository ? This question belongs there. In the future it will help people with the same questions finding the answer there. 🙏

jfly commented 2 years ago

Oops, sorry! Reading through https://docs.github.com/en/issues/tracking-your-work-with-issues/transferring-an-issue-to-another-repository, I don't think I have the power to transfer this issue. I can close this and open a new issue of you like. Or perhaps you or another admin could transfer the issue?

josephmancuso commented 2 years ago

Theres currently no way to do this now but it COULD be possible by getting the values from the databases cursor object

josephmancuso commented 2 years ago

I would assume this would be a breaking change though if we changed the delete method to return the number of rows affected

stoutput commented 2 years ago

Yes, for a pymysql connection you could use something like: connection.get_cursor().rowcount

josephmancuso commented 2 years ago

I will work on this one but it won't be available until Masonite ORM 3 around September since this is changing existing functionality in a way that could not be backwards compatable

josephmancuso commented 2 years ago

This wil also be a bit tricky because the SoftDeleteMixin changes the DELETE to an UPDATE. so will need to figure out how to get the results for an update clause as well