1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 02:49:59 +00:00

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

View file

@ -140,7 +140,7 @@
<div class="panel-heading"> <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"> <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"> <h3 class="panel-title">
Comments - {{ comments | length }} Comments - {{ torrent.comment_count }}
</h3> </h3>
</a> </a>
</div> </div>

View file

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