Skip to content

render_table error with with_entities query filter #371

@Ornanovitch

Description

@Ornanovitch

Hello! Thanks for this beautiful library :)

I got an unexpected error when trying to use the table utility. Here is some context:

vue:

def board():
    profiles = models.Profile.query.filter(
        models.db.or_(
            models.Profile.status==models.Profile.REQUEST,
            models.Profile.status==models.Profile.BLOCKED,
            models.Profile.status==models.Profile.UNCLAIMED,
        )).order_by(models.Profile.status.desc())
    profiles = profiles.with_entities(models.Profile.uuid, models.Profile.username).all()
    events = models.History.query.all()
    return flask.render_template("moderation_home.html", events=events,
                                 profiles=profiles)
template (working 🟢)

<table class="table table-striped table-head-fixed text-nowrap">
  <thead>
    <tr>
      <th>{% trans %}UUID{% endtrans %}</th>
      <th>{% trans %}username{% endtrans %}</th>
    </tr>
  </thead>
  <tbody>
    {% for profile in profiles %}
    <tr>
      <td>{{ profile.uuid }}</td>
      <td>{{ profile.username }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

template (not working 🔴)

{{ render_table(profiles, primary_key='uuid') }}

error:

Traceback (most recent call last):
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 199, in _key_not_found
    self._key_fallback(key, None)
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 138, in _key_fallback
    raise KeyError(key) from err
KeyError: '__table__'

The above exception was the direct cause of the following exception:

  File "/dev/FELINN/hiboo/hiboo/moderation/templates/moderation_home.html", line 12, in block 'content'
    {{ render_table(profiles, primary_key='uuid') }}
  File "lib/python3.12/site-packages/jinja2/runtime.py", line 782, in _invoke
    rv = self._func(*arguments)
         ^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/flask_bootstrap/templates/base/table.html", line 48, in template
    {% set titles = get_table_titles(data, primary_key, primary_key_title) %}
  File "lib/python3.12/site-packages/flask_bootstrap/__init__.py", line 26, in get_table_titles
    for k in data[0].__table__.columns.keys():
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/sqlalchemy/cyextension/resultproxy.pyx", line 66, in sqlalchemy.cyextension.resultproxy.BaseRow.__getattr__
  File "lib/sqlalchemy/cyextension/resultproxy.pyx", line 63, in sqlalchemy.cyextension.resultproxy.BaseRow._get_by_key_impl
  File "lib/python3.12/site-packages/sqlalchemy/engine/result.py", line 201, in _key_not_found
    raise AttributeError(ke.args[0]) from ke
AttributeError: __table__. Did you mean: '__le__'?

I double checked that uuid is indeed the primary key, but the result is the same with or without primary_key='uuid'.

The error appeared after I introduced

profiles = profiles.with_entities(models.Profile.uuid, models.Profile.username).all()

in my request. Without that column filter, everything works very well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions