Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609796
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:02:23+00:00 2026-05-22T01:02:23+00:00

Situation: I have two tables, say ‘master’ and ‘detail’, where ‘master’ has two columns

  • 0

Situation: I have two tables, say ‘master’ and ‘detail’, where ‘master’ has two columns that refer to ‘detail’: ‘foo_id’, ‘bar_id’. That is, I need to join detail twice with different names. I want to do:

SELECT master.id, foo.name, bar.name, other stuff ...
FROM master
JOIN detail AS foo ON foo.id = master.foo_id
JOIN detail AS bar ON bar.id = master.bar_id

how do I do that using SQLAlchemy?

Note that I am not using ORM. Also I am refering to database objects from metadata (strings), therefore I do: table.c[“foo_id”] instead of table.c.foo_id (if this information is going to be relevant to the statement construction).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:02:24+00:00Added an answer on May 22, 2026 at 1:02 am

    Fighting for quite a long time with the problem, i’ve solved it couple of minutes after posting my question here. The solution was to store and then reuse all aliased tables. Before I was using alias in join and then same alias for table to fetch column reference in SELECT, which resulted in redundant and broken ‘FROM detail AS foo, detail AS bar, master JOIN detail … JOIN detail …”

    Working solution:

    create an empty dictionary for aliased tables: { “table name”: Table object }
    for each master-detail join register new table with an alias in the dictionary:

    detail_table = Table(name,...).alias(alias) tables[alias] = detail_table
    

    join next aliased table:

    expression = join(expression, detail_table)
    

    When collecting fields for SELECT, do not get another Table(name, …) but get from your aliased table list:

    column = tables[table_name].c[column_name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I have two entities that share a primary key
I have a situation where I have two models, companies and permissions, where companies
Imagine a situation, I have PC with two lan cards, one is connected to
I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems
Situation: I have a simple XML document that contains image information. I need to
I have the following situation: I have a certain function that runs a loop
The situation: I have a pieceofcrapuous laptop. One of the things that make it
I have following situation: I have loged user, standard authentication with DB table $authAdapter
My situation: I have several components, which sometimes have changes to them, and are
My Situation I have a N rectangles The rectangles all have the same shape

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.