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

Sco*_*合理论 6 python orm unit-testing sqlalchemy object

如果我将CapacityMinclass和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'>'
Run Code Online (Sandbox Code Playgroud)

但这并不好.

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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 11

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

我怀疑你是这样用的:

from Entities import CapacityMin
Run Code Online (Sandbox Code Playgroud)

而你打算使用:

from Entities.CapacityMin import CapacityMin
Run Code Online (Sandbox Code Playgroud)

这种混淆是Python样式指南(PEP 8)建议为模块使用小写名称的原因之一; 你的导入将是:

from entities.capacitymin import CapacityMin
Run Code Online (Sandbox Code Playgroud)

而你的错误会更容易被发现.

  • 我的意思是文件名和目录名。目录构成包,文件构成模块。因此,将`Entities` 重命名为`entities`,将`CapacityMin.py` 重命名为`capacitymin.py`。 (2认同)