python - Unexpected Error in SQLAlchemy query -


i attempting create query order list of puppies based on weight, not working , receive following error

sqlalchemy.exc.invalidrequesterror: sql expression, column, or mapped entity exp ected - got '<database_setup.puppy object @ 0x02b7e590>' 

other variables same class ordered appropriately without error, code looks follows

from sqlalchemy import create_engine sqlalchemy.orm import sessionmaker  database_setup import * #from flask.ext.sqlalchemy import sqlalchemy random import randint import datetime import random   engine = create_engine('sqlite:///puppies.db')  base.metadata.bind = engine  dbsession = sessionmaker(bind=engine)  session = dbsession()   #format query  #session.query(*table*)self.filter_by(*criteria*)  # 1. query of puppies , return results in ascending alphabetical order  #so call query on shelter table order name sheltersalphabetically = session.query(shelter).order_by(shelter.name.desc())  shelter in sheltersalphabetically:     print shelter.name  print '\n' # 2. query of puppies less 6 months old organized youngest first today = datetime.date.today()   puppiesbyage = session.query(puppy).filter(puppy.dateofbirth > '2014-12-03').order_by(puppy.dateofbirth)  puppy in puppiesbyage:     print puppy.dateofbirth  print '\n'  # 3. query puppies ascending weight  puppiesbyweight = session.query(puppy).order_by(puppy.weight)  puppy in puppiesbyweight:     print puppy.weight 

my original set file looks follows

import os  import sys #provides key functions , variables  sqlalchemy import column, foreignkey, integer, string #good mapper ode  sqlalchemy.ext.declarative import declarative_base #used config , class code  sqlalchemy.orm import relationship #for foreign key relationships n mampper  sqlalchemy import create_engine  # config code  base = declarative_base() # instance of class jsut improted,                           #lets sql alchemy know our classes special                            #sql alchemy classes, correspond tables in databae  class shelter (base):     __tablename__ =  'shelter'     name = column ( string(200), nullable = false)     id = column (integer, primary_key = true)     address = column ( string(200), nullable = true)     city = column ( string(100), nullable = true)     state = column ( string(100), nullable = true)     zipcode = column ( string(100), nullable = true)     website = column ( string(200), nullable = true)  class puppy (base):     __tablename__ = 'puppy'     name = column ( string(80), nullable = false)     id = column (integer, primary_key = true)     dateofbirth = column ( integer, nullable = true)     breed = column ( string(100), nullable = true)     gender = column ( string(100), nullable = true)     picture = column ( string(300), nullable = true)     weight = column ( integer, nullable = true)     shelter_id = column ( integer, foreignkey('shelter.id'))     shelter = relationship(shelter)  #database creation , class integration engine = create_engine('sqlite:///puppies.db')  base.metadata.create_all(engine) 

what can avoid error , perform query order weight?

do not use local variable name puppy - name taken puppy class import.

i suggest use puppy (all lower case) variables in code represent puppy.

local variables in python should convention use lower case: https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -