I have two tables defined in my ORM as:
Base = declarative_base()
class GeneralLedger(Base):
__tablename__ = 'generalledgers'
id = Column(Integer, primary_key=True)
invoiceId = Column(Integer)
..
class ConsolidatedLedger(Base):
__tablename__ = 'consolidatedledgers'
id = Column(Integer, primary_key = True)
invoiceId = Column(Integer)
..
I don't have any relationship set between the two tables. I do a join as follows:
records = DBSession.query(GeneralLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).all()
I've also tried:
records = DBSession.query(GeneralLedger).filter(GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).all()
In both cases, when I display the results in my view, only the entries from the GeneralLedger table show up. How do I get results from both tables in the same result set? I've tried this:
records = DBSession.query(GeneralLedger, ConsolidatedLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).all()
But, for some reason, when I iterate through the results in my template (Jinja2), the values for the columns are empty for every single row. Also, when count:
total = DBSession.query(GeneralLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).count()
The total rows is the sum of the matching records from the two tables. I'm using webhelpers.paginate to handling paging:
query = DBSession.query(GeneralLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId)
records = paginate.Page(query, current_page, url=page_url)
and the result set sent to the template is as if all the results where there but the ones on the ConslidatedLedger table are removed. For example, I have my page total set to 20 records. If there are records from ConslidatedLedger on that page, the page is truncated, only showing records from GeneralLedger but the paging isn't broken.
Any thoughts? Thanks!
How do I join two tables in Sqlalchemy? Python Flask and SQLAlchemy ORM Now we use the join() and outerjoin() methods. The join() method returns a join object from one table object to another. For example, following use of join() method will automatically result in join based on the foreign key.
As the documentation says, all() returns the result of the query as a list.
The __repr__ function is defined by the designer of a type, in order to provide a means for users of the type to represent values of that type unambiguously, with a string.
records = DBSession.query(GeneralLedger, ConsolidatedLedger).join(ConsolidatedLedger, GeneralLedger.invoiceId == ConsolidatedLedger.invoiceId).all()
should work but I think when working with the recordset you need to refer to them via records.GeneralLedger
and records.ConsolidatedLedger
:
for record in records:
print record.GeneralLedger
print record.ConsolidatedLedger
print record.GeneralLedger.foo
# ...etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With