Flask SqlAlchemy many to many relationship only returns one result when accessing by relationship name
Your problem is with is_favorite_of
. It doesn't incorporate user
into the check. You need to add another filter.
def is_favorite_of(self, user): query = File.query query = query.join(file_favorites) query = query.filter(file_favorites.c.file_id == self.id) query = query.filter(file_favorites.c.user_id == user.id) return query.count() > 0
Alternatively, this whole function can be simplified to:
def is_favorite_of(self, user): return user in self.favorited_by