【发布时间】:2019-05-24 01:57:48
【问题描述】:
我在数据库中保存了一些电报数据,但是当我尝试使用 SESSION.add(cursor) 时,我收到了这个错误:
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped
这是我的数据库 init 文件:
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from tg_userbot import DB_URI
def start() -> scoped_session:
engine = create_engine(DB_URI, client_encoding="utf8")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
BASE = declarative_base()
SESSION = start()
这是我的表类:
class STATS(BASE):
__tablename__ = "stats"
totaldialogs = Column(Integer, primary_key=True)
usercount = Column(Integer)
channelcount = Column(Integer)
supcount = Column(Integer)
convertedgroups = Column(Integer)
numchannel = Column(Integer)
numuser = Column(Integer)
numchat = Column(Integer)
numsuper = Column(Integer)
def __init__(
self, totaldialogs, usercount, channelcount, supcount,
convertedgroups, numchannel, numuser, numchat, numsuper,
):
self.totaldialogs = totaldialogs
self.usercount = usercount
self.channelcount = channelcount
self.supcount = supcount
self.convertedgroups = convertedgroups
self.numchannel = numchannel
self.numuser = numuser
self.numchat = numchat
self.numsuper = numsuper
这是我尝试添加一些值:
db = SESSION.query(STATS).first()
if not db:
STATS(0, 0, 0, 0, 0, 0, 0, 0, 0)
SESSION.add(db)
SESSION.commit()
现在,当我使用 sql GUI 工具手动插入一些值时,添加的值就好了。但是如果表是空的,就会出现错误。
【问题讨论】:
标签: python python-3.x sqlalchemy