问题描述
如果我将CapacityMin
类和unittest类放在同一个.py文件中,那么一切都很好.但是,当我将CapacityMin
类移动到一个单独的文件并运行unit-test之后,出现了此错误:
If I put the CapacityMin
class and unittest class in same .py file, every things fine.But after I move CapacityMin
class to a separate file, and run unit-test, I got this error:
期望的SQL表达式,列或映射的实体
详细信息:
InvalidRequestError: SQL expression, column, or mapped entity expected - got '<module 'Entities.CapacityMin' from 'D:\trunk\AppService\Common\Entities\CapacityMin.pyc'>'
但这不好.
CapacityMin.py :
import sqlalchemy
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class CapacityMin(Base):
'''
table definition:
ID INT NOT NULL auto_increment,
Server VARCHAR (20) NULL,
FeedID VARCHAR (10) NULL,
`DateTime` DATETIME NULL,
PeakRate INT NULL,
BytesRecv INT NULL,
MsgNoSent INT NULL,
PRIMARY KEY (ID)
'''
__tablename__ = 'capacitymin'
ID = Column(Integer, primary_key=True)
Server = Column(String)
FeedID = Column(String)
DateTime = Column(sqlalchemy.DateTime)
PeakRate = Column(Integer)
BytesRecv = Column(Integer)
MsgNoSent = Column(Integer)
def __init__(self, server, feedId, dataTime, peakRate, byteRecv, msgNoSent):
self.Server = server
self.FeedID = feedId
self.DateTime = dataTime
self.PeakRate = peakRate
self.BytesRecv = byteRecv
self.MsgNoSent = msgNoSent
def __repr__(self):
return "<CapacityMin('%s','%s','%s','%s','%s','%s')>" % (self.Server, self.FeedID ,
self.DateTime ,self.PeakRate,
self.BytesRecv, self.MsgNoSent)
if __name__ == '__main__':
pass
推荐答案
您正在使用模块,而不是模块中的类.
You are using the module, not the class within the module.
我怀疑您是这样使用它的:
I suspect that you are using it like this:
from Entities import CapacityMin
您打算使用时:
from Entities.CapacityMin import CapacityMin
这种混淆是 Python样式指南(PEP 8)推荐的原因之一为模块使用小写名称;那么您的导入应为:
This kind of confusion is one of the reasons that the Python styleguide (PEP 8) recommends using lowercase names for your modules; your import would then be:
from entities.capacitymin import CapacityMin
您的错误将更容易发现.
and your error would have been easier to spot.
这篇关于python sqlAlchemy:更改类位置后得到InvalidRequestError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!