Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sqlAlchemy: got InvalidRequestError after change class location

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 expression, column, or mapped entity expected

DETAILS:

InvalidRequestError: SQL expression, column, or mapped entity expected - got '<module 'Entities.CapacityMin' from 'D:\trunk\AppService\Common\Entities\CapacityMin.pyc'>'

but this is not good.

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
like image 699
Scott 混合理论 Avatar asked Aug 10 '12 09:08

Scott 混合理论


1 Answers

You are using the module, not the class within the module.

I suspect that you are using it like this:

from Entities import CapacityMin

while you meant to use:

from Entities.CapacityMin import CapacityMin

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.

like image 192
Martijn Pieters Avatar answered Oct 08 '22 13:10

Martijn Pieters