gpt4 book ai didi

python sqlAlchemy : got InvalidRequestError after change class location

转载 作者:太空狗 更新时间:2023-10-29 22:16:31 24 4
gpt4 key购买 nike

如果我将 CapacityMin 类和单元测试类放在同一个 .py 文件中,一切都很好。但是在我将 CapacityMin 类移动到一个单独的文件并运行单元测试后,我得到了这个错误:

需要 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

最佳答案

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

我怀疑你是这样使用它的:

from Entities import CapacityMin

当你打算使用时:

from Entities.CapacityMin import CapacityMin

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

from entities.capacitymin import CapacityMin

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

关于 python sqlAlchemy : got InvalidRequestError after change class location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com