python sqlAlchemy: got InvalidRequestError after change class location

16,584

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.

Share:
16,584
Scott 混合理论
Author by

Scott 混合理论

**Wall is one of the most notorious inventions in the history of Internet. Anyone working for it should be ashamed. Programmer, Beijing, China using: C++ Python SqlServer windows my blog: http://www.cnblogs.com/scottgu/

Updated on June 09, 2022

Comments

  • Scott 混合理论
    Scott 混合理论 about 2 years

    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