Skip to content
Advertisement

User permission sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()

I can use below code to control the authority of menu 15,the role_id’s is_edit is 1, the use can change the data in that menu, or user doesn’t have access to change/edit data,but the code is ugly, and I only can control one menu.

JavaScript

How to control all menu? Maybe they look like this?

JavaScript

I tried to write code as below, but if I didn’t filter BaseMenuAuthority.menu_id, I will got the error of sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()

JavaScript

Advertisement

Answer

Your code seems to work as expected. Clearly, without the filter by menu_id there might be more than 1 row returned, and according to the documentation on sqlalchemy.orm.Query.one, the MultipleResultsFound exception is expected to be raised.


It looks to me that you could re-use the code of the query without (yet) executing it.

JavaScript

when you need to actually use it, you could just amend the query and execute as below

JavaScript

In fact, you could even make a helper function:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement