<p>I encountered the same issue and I applied van's answer but it did not work. However I agree with van's explanation so I added <code>.distinct()</code> to the query like this:</p>

<pre><code>old_votes = models.Papers.query.distinct().join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id).paginate(1, 4, False)
</code></pre>

<p>It worked as I expected.</p>
sqlalchemy: paginate does not return the expected number of elements
<p>I am using flask-sqlalchemy together with a sqlite database. I try to get all votes below date1</p>

<pre><code>sub_query = models.VoteList.query.filter(models.VoteList.vote_datetime &lt; date1)
sub_query = sub_query.filter(models.VoteList.group_id == selected_group.id)
sub_query = sub_query.filter(models.VoteList.user_id == g.user.id)
sub_query = sub_query.subquery()

old_votes = models.Papers.query.join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id).paginate(1, 4, False)
</code></pre>

<p>where the database model for VoteList looks like this </p>

<pre><code>class VoteList(db.Model):

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
arxiv_id = db.Column(db.String(1000), db.ForeignKey('papers.arxiv_id'))
vote_datetime = db.Column(db.DateTime)

group = db.relationship("Groups", backref=db.backref('vote_list', lazy='dynamic'))
user = db.relationship("User", backref=db.backref('votes', lazy='dynamic'), foreign_keys=[user_id])

def __repr__(self):
    return '&lt;VoteList %r&gt;' % (self.id)
</code></pre>

<p>I made sure that the 'old_votes' selection above has 20 elements. If I use .all() instead of .paginate() I get the expected 20 result?
Since I used a max results value of 4 in the example above I would expect that old_votes.items has 4 elements. But it has only 2? If I increase the max results value the number of elements also increases, but it is always below the max result value? Paginate seems to mess up something here?
any ideas?
thanks
carl</p>

<p>EDIT
I noticed that it works fine if I apply the paginate() function on add_columns(). So if I add (for no good reason) a column with </p>

<pre><code>old_votes = models.Papers.query.join(sub_query, sub_query.c.arxiv_id == models.Papers.arxiv_id)
old_votes = old_votes.add_columns(sub_query.c.vote_datetime).paginate(page, VOTES_PER_PAGE, False)
</code></pre>

<p>it works fine? But since I don't need that column it would still be interesting to know what goes wrong with my example above?</p>