Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQLAlchemy query filter with ".all()" does NOT return all rows

I'm trying to return all rows for a particular user_id. When I run the query #1 and #2 below it only returns the first row that it finds, even though I'm specifying .all().

When I use .with_entities as in case #3, all rows are returned as expected.

I would appreciate a second pair of eyes. Thank you.

I'm using MySQLDB (5.5.47 version), Flask 0.10.1 and SQL-Alchemy 2.1 if that matters.

  1. Returns first row only:

    result = MyUserClass.query.filter_by(user_id=self.user_id).all() 
    
  2. This also returns first row only:

    result = MyUserClass.query.filter(MyUserClass.user_id == self.user_id).all()
    
  3. Works and returns all rows as expected:

    MyUserClass.query.filter_by(user_id=self.user_id).with_entities(MyUserClass.myColumn).all()
    
like image 945
VanBasten Avatar asked Apr 16 '26 08:04

VanBasten


1 Answers

used_id column was marked as the primary key column in the class definition (but not in the MySQLDB), as Martijn Pieters pointed out. Adding a new column and making it the primary key fixed the problem.

like image 159
VanBasten Avatar answered Apr 19 '26 04:04

VanBasten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!