I have a setup in SQLAlchemy ORM (using Flask-SQLAlchemy) with Book and BookSubject classes, the latter being a many-to-many relationship (there is, of course, a Subject class, but it's not relevant to this question). I have a working query to return all books based on the date the subject was added to the database (there's a reason for this):
records = Book.query.\
filter(BookSubject.book_id == Book.id).\
filter(BookSubject.subject_id.in_([1, 12, 17])).\
filter(BookSubject.created > '2021-01-01').\
order_by(db.desc(BookSubject.created)).\
paginate(page, 50, True)
I then pass records to a Jinja2 template and do assorted stuff to display it; it works perfectly.
I'd now like to do the obvious thing and actually display the creation date (i.e. BookSubject.created, from the order_by clause), but I can't figure out how to add this to the query. Putting in add_columns((BookSubject.created).label("added")) isn't the answer; that throws an error when I try to use one of the record objects "'sqlalchemy.util._collections.result object' has no attribute 'id'". The template code that generates this is (roughly):
{% for book in records.items %}
<tr><td><a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }}</a></td></tr>
{% endfor %}
This should be obvious; how am I meant to add this to the result?
add_columnsis a correct way, the only difference it returns a named tuple (row) instead of an ORM object (Book). please provide the code which produces the last exception.paginatecall (from Flask-SQLAlchemy) in the original query that I didn't previously mention.