sqlalchemy UnmappedInstanceError:类'builtins.NoneType'未映射

时间:2018-12-25 10:05:49

标签: python python-3.x sqlalchemy

我正在将一些电报数据保存在数据库中,但是当我尝试使用// i have cloned your repo and run it successfully, by moving username // and password from index.js to SignIn.js, rewrite SignIn in class way, // navigation works well(jump to signed successfuly after pressing). // however not sure if this is what you need. import React, {Component} from "react"; import { View, Alert } from "react-native"; import { Card, Button, FormLabel, FormInput } from "react-native-elements"; export default class SignIn extends Component { constructor (props) { super(props); this.state = { username: '', password: '' } } _handlePress () { let {navigation} = this.props; navigation.navigate("SignedIn") } _handleUChange (username) { this.setState({username}) } _handlePChange (password) { this.setState({password}) } render () { return ( <View style={{ paddingVertical: 20 }}> <Card title="SIGN IN"> <FormLabel>Email</FormLabel> <FormInput placeholder="Email address..." onChangeText={this._handleUChange.bind(this)} /> <FormLabel>Password</FormLabel> <FormInput secureTextEntry placeholder="Password..." onChangeText={this._handlePChange.bind(this)} /> <Button buttonStyle={{ marginTop: 20 }} backgroundColor="#03A9F4" title="SIGN IN" onPress={this._handlePress.bind(this)} /> </Card> </View> ) } } 时出现此错误: SESSION.add(cursor)

这是我的数据库 init 文件:


sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped

这是我的桌子课:

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

现在,当我使用sql GUI工具手动插入一些值时,就可以添加值了。但是,如果表为空,则会出现错误。

1 个答案:

答案 0 :(得分:0)

我不习惯长代码,所以犯了一个大错误,我从数据库中添加了None,然后又将其重新添加。将STATS(....)分配给一个变量,然后将该变量添加为有效