0

I am trying to add a feature to a web app wherein the coordinates will automatically update when a map is dragged by means of JavaScript and SQLAlchemy. But I am getting an error with my database query for the update route. Here is my query.

if sw_lng <= ne_lng:

# doesn't cross the antimeridian
rows = City.query.filter(sw_lat <= City.latitude and City.latitude <= ne_lat and (sw_lng <= City.longitude and City.longitude <= ne_lng)
                         ).group_by(City.country_code, City.city_name, City.region
                                    ).order_by(func.random()).limit(10)

Here is the relevant error message:

raise TypeError("Boolean value of this clause is not defined")

TypeError: Boolean value of this clause is not defined

Any help will be appreciated.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
aharding378
  • 207
  • 1
  • 3
  • 16

1 Answers1

3

Don't use and to combine your filters, pass each filter expression as a separate argument (the default behavior is to treat them as and-ed together). Using and itself is trying to perform local boolean tests (Python, for good reason, doesn't allow any form of overloading that would change the meaning of and), but SQLAlchemy is using the tests as special purpose objects that it evaluates lazily to generate the SQL query at query dispatch time (pushing the work to the DB server). You want:

rows = City.query.filter(sw_lat <= City.latitude, City.latitude <= ne_lat,
                         sw_lng <= City.longitude, City.longitude <= ne_lng)...cont....
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • thanks for the help, that took care of that error. However my end goal is to return a JSON object to the client side with jsonify. – aharding378 Sep 28 '17 at 02:49
  • this is the new error I get when I try to do that o.__class__.__name__) TypeError: Object of type 'BaseQuery' is not JSON serializable – aharding378 Sep 28 '17 at 02:50
  • @aharding378: That's a completely different question, for which you haven't provided the code that triggers the error. Sounds like you're trying to return the query object itself, rather than resolving the query to actual rows, but I can't be sure. Search to see if it's been answered, ask a new question if not. – ShadowRanger Sep 28 '17 at 10:07