python sqlAlchemy:更改类位置后得到InvalidRequestError

时间:2012-08-10 09:09:26

标签: python unit-testing object orm sqlalchemy

如果我将CapacityMin类和unittest类放在同一个.py文件中,那么一切都很好。 但是在我将CapacityMin类移动到一个单独的文件并运行unit-test之后,我收到了这个错误:

期望的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

1 个答案:

答案 0 :(得分:11)

您正在使用模块,而不是模块中的类。

我怀疑你是这样用的:

from Entities import CapacityMin

虽然您打算使用:

from Entities.CapacityMin import CapacityMin

这种混淆是Python styleguide (PEP 8)建议为模块使用小写名称的原因之一;那么你的导入就是:

from entities.capacitymin import CapacityMin

并且您的错误会更容易被发现。