Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing Duplicate Table Names in SQL Queries Using SQLAlchemy #27

Closed
neatek opened this issue Mar 15, 2024 · 3 comments · Fixed by #29
Closed

Preventing Duplicate Table Names in SQL Queries Using SQLAlchemy #27

neatek opened this issue Mar 15, 2024 · 3 comments · Fixed by #29
Assignees
Labels
bug Something isn't working

Comments

@neatek
Copy link

neatek commented Mar 15, 2024

Thank you for the recent update! We appreciate the enhancements and improvements made. It's not critical, but I think it's worth discussing.

Describe the bug or question
The problem lies in encountering an error due to duplicate table names when writing SQL queries using SQLAlchemy. This occurs when there's ambiguity in the query because the same table name is used multiple times without explicitly specifying aliases. As a result, SQLAlchemy fails to process the query correctly, leading to a query execution error.

To Reproduce

booking_join_config = [
    JoinConfig(
        model=models.UserModel,
        join_on=models.Booking.owner_id == models.UserModel.id,
        join_prefix="owner_",
        schema_to_select=schemas.UserBase,
        join_type="inner",
    ),
    JoinConfig(
        model=models.UserModel,
        join_on=models.Booking.user_id == models.UserModel.id,
        join_prefix="user_",
        schema_to_select=schemas.UserBase,
        join_type="inner",
    ),
]

Description
Output: table name "users" specified more than once

FROM bookings 
JOIN users ON bookings.owner_id = users.id 
JOIN users ON bookings.user_id = users.id 

Additional context

FROM bookings 
JOIN users AS owner_users ON bookings.owner_id = owner_users.id 
JOIN users AS user_users ON bookings.user_id = user_users.id 
@igorbenav igorbenav self-assigned this Mar 15, 2024
@igorbenav igorbenav added the bug Something isn't working label Mar 15, 2024
@igorbenav
Copy link
Owner

Thanks for the issue! I forgot to test for this indeed, will fix as soon as possible

@igorbenav
Copy link
Owner

Fix for this is basically ready, I'll just write the docs and upload later today

@igorbenav
Copy link
Owner

Hey, @neatek, check the docs for aliases here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants