← Back to team overview

caneypuggies team mailing list archive

Fixed lat/lng comparison in query

 

Hi Doug, and other CaneyPUGgies members,

I was able to fix the errors we were getting tonight trying to pull
Unicode data out of the lat/lng fields of the Congregation table, then
numerically compare it with Unicode strings in the query's filter
section using greater or less than signs, like this:

        # Query the data base using cong_lat > south_lat and cong_lat <
north_lat and cong_lng > west_lng and cong_lng < east_lng
        # and make a list of all congregations that fall within the
parameters specified.
        congs = DBSession.query(model.Congregation).filter(
                  and_(
                     model.Congregation.lat > south_lat,
                     model.Congregation.lat < north_lat,
                     model.Congregation.lng > west_lng,
                     model.Congregation.lng < east_lng
                     )
                  ).all()

Python probably automatically converted south_lat (which started as a
Unicode string data type) to a floating point number data type to do the
> comparison, but for some reason Python would not convert
model.Congregation.lat from an InstrumentedAttribute (or maybe Unicode
string) data type to a floating point number.

The problem was that we started with the wrong data type in the
database.  So, the solution was to create a database migration that
changed the Congregation.lat and Congregation.lng fields from an
SQLAlchemy Unicode to a Numeric (returned as Decimal) data type, which
means now model.Congregation.lat (and .lng) above are the SQLAlchemy
Decimal data type, and can be compared with the data type of south_lat
and the other variables on the right side of each comparison.  The
migration, and other necessary changes, are in this changeset: 
http://bazaar.launchpad.net/~caneypuggies/reformedchurcheslocator/trunk/revision/99.

Everyone will need to run the new migration after pulling it into their
local repositories via Bazaar.  The commands to type in your terminal to
do so are between the dotted lines below.

#----------------------

cd ~/Projects/reformedchurcheslocator
bzr pull
source ../tg21env/bin/activate
pip install --upgrade sqlalchemy-migrate
./manage.py upgrade

#----------------------

Tim