0

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.

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

//SqlAlchmey
return UserBeer.query.filter(or_( UserBeer.id < 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 << self.following()) |
            (UserBeer.username == self)).order_by(UserBeer.tried_date.desc()).limit(5)

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

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

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.

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'))

EDIT I thought the whole error might be useful when I was looking it over again

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 < (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)]
Cœur
  • 37,241
  • 25
  • 195
  • 267
bobthemac
  • 1,172
  • 6
  • 26
  • 59
  • What is ``self.following()``? – turkus Oct 07 '16 at 21:15
  • its a function in my user model that returns users following that user like twitter all it does is pass out the two querys under that bit are the content, well to versions one when I was using peewee and the other now I am converting to sqlalchemy – bobthemac Oct 07 '16 at 21:18
  • Why are you switching from flask-peewee to flask-sqlalchemy, if you don't mind my asking? :) – coleifer Oct 10 '16 at 19:36

2 Answers2

2

OperationalError: (_mysql_exceptions.OperationalError) (1241, 'Operand should contain 1 column(s)')

tells you that you're trying to compare a list of columns to one user id column.

return UserBeer.query.filter(or_( UserBeer.id < self.following(), UserBeer.username == self)).order_by(UserBeer.tried_date.desc()).limit(5)

in the code above UserBeer.id is one column value and self.following() is multiple column value.

turkus
  • 4,637
  • 2
  • 24
  • 28
  • Just tried that and I get some error about base query not having an id attribute – bobthemac Oct 07 '16 at 21:51
  • Ah I see the problem now in I forgot that `<<` was and IN when using peewee, not less than. Would the in_() function be what I use here I still get the id attribute error after calling it. It's not the id I should be looking for anyway just changed it to the plain username and now get a not implemented error. Thanks for your help – bobthemac Oct 07 '16 at 22:09
0

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

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.

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

coleifer
  • 24,887
  • 6
  • 60
  • 75