简体   繁体   中英

Adding related column to query results?

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 (ie 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?

By using add_columns

Book.query.add_columns((BookSubject.created).label("added"))

it will return a named tuple with fields Book and added , so to access book fields you'd need something like book.Book.id

{% for book in records.items %}
  <tr><td>
     <a href="{{ url_for('fullview', id=book.Book.id) }}">{{ book.Book.title }} {{ book.added }} </a>
  </td></tr>
{% endfor %}

or iterate by pairs:

{% for book, added in records.items %}
  <tr><td>
     <a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }} {{ added }} </a>
  </td></tr>
{% endfor %}

If you want a flat structure (and without add_columns ), then you can use

session.query(
  Book.id,
  Book.title,
  BookSubject.created.label('added')
).filter(BookSubject.book_id == Book.id)...

then results will have a named tuple with fields id , title and added , so you can print directly book.id :

{% for book in records.items %}
  <tr><td>
    <a href="{{ url_for('fullview', id=book.id) }}">{{ book.title }} {{ book.added }} </a>
  </td></tr>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM