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 3690858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:04:57+00:00 2026-05-19T04:04:57+00:00

I’m a newcomer to SQLAlchemy ORM and I’m struggling to accomplish complex-ish queries on

  • 0

I’m a newcomer to SQLAlchemy ORM and I’m struggling to accomplish complex-ish queries on multiple tables – queries which I find relatively straightforward to do in Doctrine DQL.

I have data objects of Cities, which belong to Countries. Some Cities also have a County ID set, but not all. As well as the necessary primary and foreign keys, each record also has a text_string_id, which links to a TextStrings table which stores the name of the City/County/Country in different languages. The TextStrings MySQL table looks like this:

CREATE TABLE IF NOT EXISTS `text_strings` (
    `id` INT UNSIGNED NOT NULL,
    `language` VARCHAR(2) NOT NULL,
    `text_string` varchar(255) NOT NULL,
    PRIMARY KEY (`id`, `language`)
)

I want to construct a breadcrumb for each city, of the form:

country_en_name > city_en_name OR

country_en_name > county_en_name > city_en_name,

depending on whether or not a County attribute is set for this city. In Doctrine this would be relatively straightforward:

    $query = Doctrine_Query::create()
                ->select('ci.id, CONCAT(cyts.text_string, \'> \', IF(cots.text_string is not null, CONCAT(cots.text_string, \'> \', \'\'), cits.text_string) as city_breadcrumb')
                ->from('City ci')
                ->leftJoin('ci.TextString cits')
                ->leftJoin('ci.Country cy')
                ->leftJoin('cy.TextString cyts')
                ->leftJoin('ci.County co')
                ->leftJoin('co.TextString cots')
                ->where('cits.language = ?', 'en')
                ->andWhere('cyts.language = ?', 'en')
                ->andWhere('(cots.language = ? OR cots.language is null)', 'en');

With SQLAlchemy ORM, I’m struggling to achieve the same thing. I believe I’ve setup the objects correctly – in the form eg:

class City(Base):
    __tablename__ = "cities"

    id = Column(Integer, primary_key=True)
    country_id = Column(Integer, ForeignKey('countries.id'))
    text_string_id = Column(Integer, ForeignKey('text_strings.id'))
    county_id = Column(Integer, ForeignKey('counties.id'))

    text_strings = relation(TextString, backref=backref('cards', order_by=id))
    country = relation(Country, backref=backref('countries', order_by=id))
    county = relation(County, backref=backref('counties', order_by=id))

My problem is in the querying – I’ve tried various approaches to generating the breadcrumb but nothing seems to work. Some observations:

Perhaps using things like CONCAT and IF inline in the query is not very pythonic (is it even possible with the ORM?) – so I’ve tried performing these operations outside SQLAlchemy, in a Python loop of the records. However here I’ve struggled to access the individual fields – for example the model accessors don’t seem to go n-levels deep, e.g. City.counties.text_strings.language doesn’t exist.

I’ve also experimented with using tuples – the closest I’ve got to it working was by splitting it out into two queries:

# For cities without a county
for city, country in session.query(City, Country).\
    filter(Country.id == City.country_id).\
    filter(City.county_id == None).all():

    if city.text_strings.language == 'en':
    # etc

# For cities with a county
for city, county, country in session.query(City, County, Country).\
    filter(and_(City.county_id == County.id, City.country_id == Country.id)).all():

    if city.text_strings.language == 'en':
    # etc

I split it out into two queries because I couldn’t figure out how to make the Suit join optional in just the one query. But this approach is of course terrible and worse the second query didn’t work 100% – it wasn’t joining all of the different city.text_strings for subsequent filtering.

So I’m stumped! Any help you can give me setting me on the right path for performing these sorts of complex-ish queries in SQLAlchemy ORM would be much appreciated.

  • 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-19T04:04:57+00:00Added an answer on May 19, 2026 at 4:04 am

    The mapping for Suit is not present but based on the propel query I would assume it has a text_strings attribute.

    The relevant portion of SQLAlchemy documentation describing aliases with joins is at:

    http://www.sqlalchemy.org/docs/orm/tutorial.html#using-aliases

    generation of functions is at:

    http://www.sqlalchemy.org/docs/core/tutorial.html#functions

    cyts = aliased(TextString)
    cits = aliased(TextString)
    cots = aliased(TextString)
    cy = aliased(Suit)
    co = aliased(Suit)
    
    session.query(
                City.id, 
                (
                    cyts.text_string + \
                    '> ' + \
                    func.if_(cots.text_string!=None, cots.text_string + '> ', cits.text_string)
                ).label('city_breadcrumb')
                ).\
                outerjoin((cits, City.text_strings)).\
                outerjoin((cy, City.country)).\
                outerjoin((cyts, cy.text_strings)).\
                outerjoin((co, City.county))\
                outerjoin((cots, co.text_string)).\
                filter(cits.langauge=='en').\
                filter(cyts.langauge=='en').\
                filter(or_(cots.langauge=='en', cots.language==None))
    

    though I would think its a heck of a lot simpler to just say:

    city.text_strings.text_string + " > " + city.country.text_strings.text_string + " > " city.county.text_strings.text_string
    

    If you put a descriptor on City, Suit:

    class City(object):
       # ...
       @property
       def text_string(self):
          return self.text_strings.text_string
    

    then you could say city.text_string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.