Skip to content

Commit d7bad26

Browse files
Merge pull request #968 from aniket866/queries
backend/models.py: N+1 query problem when fetching books and their associated authors
2 parents 2c0bdb2 + b98924b commit d7bad26

2 files changed

Lines changed: 99 additions & 15 deletions

File tree

‎backend/models.py‎

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ class PasswordResetToken(db.Model):
141141
used_at = db.Column(db.DateTime, nullable=True)
142142
created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
143143

144-
user = db.relationship('User', backref=db.backref('password_reset_tokens', lazy='dynamic'))
144+
user = db.relationship(
145+
'User',
146+
backref=db.backref('password_reset_tokens', lazy='dynamic'),
147+
lazy='joined'
148+
)
145149

146150

147151
class Book(db.Model, SoftDeleteMixin):
@@ -306,9 +310,22 @@ def validate_shelf_type(self, key, value):
306310
return value
307311

308312
# Relationships
309-
user = db.relationship('User', backref=db.backref('shelf_items', lazy=True))
310-
book = db.relationship('Book', backref=db.backref('shelf_items', lazy=True))
311-
price_alerts = db.relationship('PriceAlert', backref='shelf_item', lazy=True, cascade='all, delete-orphan')
313+
user = db.relationship(
314+
'User',
315+
backref=db.backref('shelf_items', lazy='subquery'),
316+
lazy='joined'
317+
)
318+
book = db.relationship(
319+
'Book',
320+
backref=db.backref('shelf_items', lazy='subquery'),
321+
lazy='joined'
322+
)
323+
price_alerts = db.relationship(
324+
'PriceAlert',
325+
backref=db.backref('shelf_item', lazy='joined'),
326+
lazy='subquery',
327+
cascade='all, delete-orphan'
328+
)
312329

313330
# =========================================================================
314331
# DATABASE LEVEL CONSTRAINTS
@@ -403,7 +420,11 @@ class ReadingGoal(db.Model, SoftDeleteMixin):
403420
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
404421

405422
# Relationships
406-
user = db.relationship('User', backref=db.backref('reading_goals', lazy=True))
423+
user = db.relationship(
424+
'User',
425+
backref=db.backref('reading_goals', lazy='subquery'),
426+
lazy='joined'
427+
)
407428

408429
__table_args__ = (
409430
db.UniqueConstraint('user_id', 'year', name='uq_user_year_goal'),
@@ -434,7 +455,11 @@ class ReadingStats(db.Model, SoftDeleteMixin):
434455
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
435456

436457
# Relationships
437-
user = db.relationship('User', backref=db.backref('reading_stats', lazy=True))
458+
user = db.relationship(
459+
'User',
460+
backref=db.backref('reading_stats', lazy='subquery'),
461+
lazy='joined'
462+
)
438463

439464
__table_args__ = (
440465
db.UniqueConstraint('user_id', 'year', 'month', name='uq_user_year_month_stats'),
@@ -466,8 +491,17 @@ class Collection(db.Model, SoftDeleteMixin):
466491
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
467492

468493
# Relationships
469-
user = db.relationship('User', backref=db.backref('collections', lazy=True))
470-
items = db.relationship('CollectionItem', backref='collection', lazy=True, cascade='all, delete-orphan')
494+
user = db.relationship(
495+
'User',
496+
backref=db.backref('collections', lazy='subquery'),
497+
lazy='joined'
498+
)
499+
items = db.relationship(
500+
'CollectionItem',
501+
backref=db.backref('collection', lazy='joined'),
502+
lazy='subquery',
503+
cascade='all, delete-orphan'
504+
)
471505

472506
__table_args__ = (
473507
db.UniqueConstraint('user_id', 'name', name='uq_user_collection_name'),
@@ -499,7 +533,11 @@ class CollectionItem(db.Model, SoftDeleteMixin):
499533
added_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
500534

501535
# Relationships
502-
book = db.relationship('Book', backref=db.backref('collection_items', lazy=True))
536+
book = db.relationship(
537+
'Book',
538+
backref=db.backref('collection_items', lazy='subquery'),
539+
lazy='joined'
540+
)
503541

504542
__table_args__ = (
505543
db.UniqueConstraint('collection_id', 'book_id', name='uq_collection_book'),
@@ -565,7 +603,11 @@ class PriceHistory(db.Model, SoftDeleteMixin):
565603
checked_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
566604

567605
# Relationships
568-
book = db.relationship('Book', backref=db.backref('price_history', lazy=True))
606+
book = db.relationship(
607+
'Book',
608+
backref=db.backref('price_history', lazy='subquery'),
609+
lazy='joined'
610+
)
569611

570612
__table_args__ = (
571613
db.Index('idx_price_history_book_retailer', 'book_id', 'retailer'),
@@ -599,7 +641,11 @@ class PriceAlert(db.Model, SoftDeleteMixin):
599641
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
600642

601643
# Relationships
602-
user = db.relationship('User', backref=db.backref('price_alerts', lazy=True))
644+
user = db.relationship(
645+
'User',
646+
backref=db.backref('price_alerts', lazy='subquery'),
647+
lazy='joined'
648+
)
603649

604650
__table_args__ = (
605651
db.UniqueConstraint('user_id', 'shelf_item_id', name='uq_user_shelf_item_alert'),
@@ -638,8 +684,16 @@ class Review(db.Model, SoftDeleteMixin):
638684
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
639685

640686
# Relationships
641-
user = db.relationship('User', backref=db.backref('reviews', lazy=True))
642-
book = db.relationship('Book', backref=db.backref('reviews', lazy=True))
687+
user = db.relationship(
688+
'User',
689+
backref=db.backref('reviews', lazy='subquery'),
690+
lazy='joined'
691+
)
692+
book = db.relationship(
693+
'Book',
694+
backref=db.backref('reviews', lazy='subquery'),
695+
lazy='joined'
696+
)
643697

644698
__table_args__ = (
645699
db.UniqueConstraint('user_id', 'book_id', name='uq_user_book_review'),
@@ -681,8 +735,16 @@ class JournalEntry(db.Model, SoftDeleteMixin):
681735
updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
682736

683737
# Relationships
684-
user = db.relationship('User', backref=db.backref('journal_entries', lazy=True))
685-
book = db.relationship('Book', backref=db.backref('journal_entries', lazy=True))
738+
user = db.relationship(
739+
'User',
740+
backref=db.backref('journal_entries', lazy='subquery'),
741+
lazy='joined'
742+
)
743+
book = db.relationship(
744+
'Book',
745+
backref=db.backref('journal_entries', lazy='subquery'),
746+
lazy='joined'
747+
)
686748

687749
def to_dict(self):
688750
return {

‎frontend/data/contributors.json‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@
219219
"site_admin": false,
220220
"contributions": 13
221221
},
222+
{
223+
"login": "github-actions[bot]",
224+
"id": 41898282,
225+
"node_id": "MDM6Qm90NDE4OTgyODI=",
226+
"avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
227+
"gravatar_id": "",
228+
"url": "https://api.github.com/users/github-actions%5Bbot%5D",
229+
"html_url": "https://github.com/apps/github-actions",
230+
"followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
231+
"following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
232+
"gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
233+
"starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
234+
"subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
235+
"organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
236+
"repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
237+
"events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
238+
"received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
239+
"type": "Bot",
240+
"user_view_type": "public",
241+
"site_admin": false,
242+
"contributions": 10
243+
},
222244
{
223245
"login": "D-e-x-t-e-r-0",
224246
"id": 215631297,

0 commit comments

Comments
 (0)