Skip to content
Advertisement

Why is my code for sqlalchemy query getting an error while raw mysql isn’t?

I am having issues using SQLAlchemy. When executing below code I get an error (1066, "Not unique table/alias: 'user_role'"). If I am to execute the query myself in mysql-console taken from the sqlalchmey query-string I get no such error. What could make sqlachemy make it so that it throws an error such as this?

UserRole is table user_role.

JavaScript

Raw mysql:

JavaScript

Advertisement

Answer

For some reason the issue wasn’t user_role but school instead. After opening the project the day after, the error issue was for ‘school’ and not ‘user_role’.

I tried adding stud_roles = orm.aliased(UserRole) before but to no avail. By adding schools = orm.aliased(School) as well my issue was resolved.

I read that every table that is in a select-query needs to be in aliased. Not 100% on why but it solved my issue.

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