AttributeError: 'Query' object has no attribute 'is_clause_element' when joining table with query

Question:

AttributeError: ‘Query’ object has no attribute ‘is_clause_element’ when joining table with query

I have a query that counts the amount of keywords a company has and then sorts them by the amount of keywords they have.

query_company_ids = Session.query(enjordplatformCompanyToKeywords.company_id.label("company_id"),func.count(enjordplatformCompanyToKeywords.keyword_id)).group_by(enjordplatformCompanyToKeywords.company_id).order_by(desc(func.count(enjordplatformCompanyToKeywords.keyword_id))).limit(20)

I then want to get information about these companies like image, title, info etc and send it to the frontend (this is done later by looping through companies_query).
Though I have trouble in building the connection between the query_company_ids query and enjordplatformCompanies table.

I have tried two ways of doing this:

  1. companies_query = Session.query(enjordplatformCompanies, query_company_ids).filter(enjordplatformCompanies.id == query_company_ids.company_id).all()
  2. companies_query = Session.query(enjordplatformCompanies, query_company_ids).join( query_company_ids, query_company_ids.c.company_id == enjordplatformCompanies.id).all()

But both of them result in the error: AttributeError: ‘Query’ object has no attribute ‘is_clause_element’

Question
How can I join the query_company_ids query and enjordplatformCompanies table?
Thanks

Here are the table definitions

class enjordplatformCompanies(Base):
    __tablename__ = "enjordplatform_companies"
    id = Column(Integer, primary_key=True,  unique=True)
    name = Column(String)
    about = Column(String)
    image = Column(String)
    website = Column(String)
    week_added = Column(Integer)
    year_added = Column(Integer)
    datetime_added = Column(DateTime)
    created_by_userid = Column(Integer)
    company_type = Column(String)
    contact_email=Column(String)
    adress=Column(String)
    city_code=Column(String)
    city=Column(String)


class enjordplatformCompanyToKeywords(Base):
    __tablename__ = "enjordplatform_company_to_keywords"
    id = Column(Integer, primary_key=True, unique=True)
    company_id = Column(Integer,ForeignKey("enjordplatform_companies.id"))
    keyword_id = Column(Integer,ForeignKey("enjordplatform_keywords.id"))
Asked By: user12288003

||

Answers:

I copied your example query above and was getting a lot of weird errors until I realized you use Session instead of session. I guess make sure you are using an instance instead of the class or sessionmaker.

Below I create an explicit subquery() to get the company id paired with its keyword count and then I join the companies class against that, applying the order and limit to the final query.

with Session(engine) as session, session.begin():
    subq = session.query(
        enjordplatformCompanyToKeywords.company_id,
        func.count(enjordplatformCompanyToKeywords.keyword_id).label('keyword_count')
    ).group_by(
        enjordplatformCompanyToKeywords.company_id
    ).subquery()
    q = session.query(
        enjordplatformCompanies,
        subq.c.keyword_count
    ).join(
        subq,
        enjordplatformCompanies.id == subq.c.company_id
    ).order_by(
        desc(subq.c.keyword_count)
    )
    for company, keyword_count in q.limit(20).all():
        print (company.name, keyword_count)

This isn’t the exact method but explains the intention of calling .subquery() above:
subquery

Answered By: Ian Wilson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.