Flask-peewee to Flask-sqlalchmey - Operand should contain 1 column(s)
<p>I am having an issue with this error Operand should contain 1 column(s), I get that is about their being two fields in the subquery but from my code and using flask-sqlalchemy I can't work out what is going wrong. I am converting my application from flask-peewee to flask-sqlalchemy and it is this one issue that I can't work out.</p>

<p>Here it the main query code the first one is my new sql-alchemy query and the other is the peewee one. </p>

<pre><code>//SqlAlchmey
return UserBeer.query.filter(or_( UserBeer.id &lt; self.following(), UserBeer.username == self)).order_by(UserBeer.tried_date.desc()).limit(5)

//Peewee Query
return UserBeer.select(Beer, UserBeer).join(Beer).where(
            (UserBeer.username &lt;&lt; self.following()) |
            (UserBeer.username == self)).order_by(UserBeer.tried_date.desc()).limit(5)
</code></pre>

<p>The part of the query that is causing the issue is the call to <code>self.following()</code> if I remove that the sqlalchemy query works here is the code for that query bellow is the contents of <code>self.following()</code></p>

<pre><code>// SQLAlchmey
return Relationship.query.filter(Relationship.from_user == self)
//Peewee
return ( User.select().join(Relationship, on=Relationship.to_user).where(Relationship.from_user == self))
</code></pre>

<p>I know the second query I am asking for two different things but it seems like you declare the relationships in the models in SQLAlchemy here is the relationship model I think it is right but not sure. Also my user model not sure if there is something missing.</p>

<pre><code>class Relationship(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    from_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    to_user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    from_user = db.relationship('User', backref=db.backref('relationships', lazy='joined'), foreign_keys=from_user_id)
    to_user = db.relationship('User', backref=db.backref('related_to', lazy='joined'), foreign_keys=to_user_id)

class User(UserMixin, db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(), unique=True)
    email = db.Column(db.String(), unique=True)
    bio = db.Column(db.String(160))
    password = db.Column(db.String(200))
    joined_at = db.Column(db.DateTime(), default=datetime.datetime.now)
    is_admin = db.Column(db.Boolean(), default=False)
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
</code></pre>

<p><strong>EDIT</strong>
I thought the whole error might be useful when I was looking it over again</p>

<pre><code>OperationalError: (_mysql_exceptions.OperationalError) (1241, 'Operand should contain 1 column(s)') [SQL: u'SELECT user_beer.id AS user_beer_id, user_beer.username_id AS user_beer_username_id, user_beer.beer_id AS user_beer_beer_id, user_beer.rating AS user_beer_rating, user_beer.description AS user_beer_description, user_beer.recommend AS user_beer_recommend, user_beer.tried_date AS user_beer_tried_date \nFROM user_beer \nWHERE user_beer.id &lt; (SELECT relationship.id AS relationship_id, relationship.from_user_id AS relationship_from_user_id, relationship.to_user_id AS relationship_to_user_id \nFROM relationship \nWHERE %s = relationship.from_user_id) OR %s = user_beer.username_id ORDER BY user_beer.tried_date DESC \n LIMIT %s'] [parameters: (1L, 1L, 5)]
</code></pre>

<p>Switching from Peewee to SQLAlchemy? Why on earth would you do such a thing!</p>

<p>Honestly, if you've run into what you believe is a bug in Peewee, or find things overly difficult or un-intuitive, that is exactly the most useful kind of feedback for me as the maintainer. Sure, it's nice hearing from people who say "Peewee is great, thanks", but it's the folks who end up leaving that generally have the most to say.</p>

<p>Please stop by and leave a comment <a href="https://github.com/coleifer/peewee/issues/new" rel="nofollow">https://github.com/coleifer/peewee/issues/new</a> or hit me up at <a href="http://charlesleifer.com/contact/" rel="nofollow">http://charlesleifer.com/contact/</a> -- I'd be extremely grateful for even a few minutes of your time.</p>

python mysql flask-sqlalchemy flask-peewee