我正在尝试创建一个查询来根据体重排序小狗列表,但是它不起作用,我收到以下错误
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity exp
ected - got '<database_setup.Puppy object at 0x02B7E590>'
来自同一个类的其他变量被正确排序而没有错误,我的代码如下所示
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import *
#from flask.ext.sqlalchemy import SQLAlchemy
from random import randint
import datetime
import random
engine = create_engine('sqlite:///puppies.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
#Format for query
#session.query(*table*)self.filter_by(*criteria*)
# 1. Query all of the puppies and return the results in ascending alphabetical order
#so we call a query on the Shelter table where we order by name
SheltersAlphabetically = session.query(Shelter).order_by(Shelter.name.desc())
for Shelter in SheltersAlphabetically:
print Shelter.name
print '\n'
# 2. Query all of the puppies that are less than 6 months old organized by the youngest first
today = datetime.date.today()
PuppiesByAge = session.query(Puppy).filter(Puppy.dateOfBirth > '2014-12-03').order_by(Puppy.dateOfBirth)
for Puppy in PuppiesByAge:
print Puppy.dateOfBirth
print '\n'
# 3. Query all puppies by ascending weight
PuppiesByWeight = session.query(Puppy).order_by(Puppy.weight)
for Puppy in PuppiesByWeight:
print Puppy.weight
我的原始设置文件如下所示
import os
import sys #provides key functions and variables
from sqlalchemy import Column, ForeignKey, Integer, String #good for mapper ode
from sqlalchemy.ext.declarative import declarative_base #used for config and class code
from sqlalchemy.orm import relationship #for foreign key relationships n mampper
from sqlalchemy import create_engine # for config code
Base = declarative_base() # an instance of class we jsut improted,
#lets sql alchemy know our classes are special
#sql alchemy classes, correspond to 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 and class integration
engine = create_engine('sqlite:///puppies.db')
Base.metadata.create_all(engine)
如何避免此错误并执行查询以订购权重?
答案 0 :(得分:0)
不要使用本地变量名Puppy
- 已为您导入的Puppy
类使用该名称。
我建议您在代码中代表小狗的变量使用puppy
(全部小写)。
Python中的局部变量应按惯例使用小写:https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables