Fix some JOIN insanity with SQLAlchemy.

This commit is contained in:
Alex Ingram 2019-04-07 16:48:08 -05:00 committed by Nicolas F
parent b0c51e9fa0
commit 75e7e942fb
3 changed files with 9 additions and 7 deletions

View File

@ -192,7 +192,7 @@ class TorrentBase(DeclarativeHelperBase):
@declarative.declared_attr
def comments(cls):
return db.relationship(cls._flavor_prefix('Comment'), uselist=True,
cascade="all, delete-orphan")
cascade="all, delete-orphan", lazy='noload')
def __repr__(self):
return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self)
@ -435,7 +435,7 @@ class CommentBase(DeclarativeHelperBase):
@declarative.declared_attr
def torrent(cls):
return db.relationship(cls._flavor_prefix('Torrent'), uselist=False,
back_populates='comments', lazy="joined")
back_populates='comments', lazy="noload")
def __repr__(self):
return '<Comment %r>' % self.id
@ -499,7 +499,7 @@ class User(db.Model):
bans = db.relationship('Ban', uselist=True, foreign_keys='Ban.user_id')
preferences = db.relationship('UserPreferences',
back_populates='user', uselist=False, lazy='joined')
back_populates='user', uselist=False, lazy='select')
def __init__(self, username, email, password):
self.username = username

View File

@ -140,7 +140,7 @@
<div class="panel-heading">
<a data-toggle="collapse" href="#collapse-comments" role="button" aria-expanded="{% if g.user and g.user.preferences.hide_comments %}false{% else %}true{% endif %}" aria-controls="collapse-comments">
<h3 class="panel-title">
Comments - {{ comments | length }}
Comments - {{ torrent.comment_count }}
</h3>
</a>
</div>

View File

@ -21,10 +21,12 @@ def view_torrent(torrent_id):
torrent = models.Torrent.by_id(torrent_id)
else:
torrent = models.Torrent.query \
.options(joinedload('filelist'),
joinedload('comments')) \
.options(joinedload('filelist')) \
.filter_by(id=torrent_id) \
.first()
comments = models.Comment.query \
.filter_by(torrent_id=torrent_id)
if not torrent:
flask.abort(404)
@ -71,7 +73,7 @@ def view_torrent(torrent_id):
return flask.render_template('view.html', torrent=torrent,
files=files,
comment_form=comment_form,
comments=torrent.comments,
comments=comments,
can_edit=can_edit,
report_form=report_form)