flask sqlalchemy尽管有

时间:2018-05-10 07:18:13

标签: python flask sqlalchemy flask-sqlalchemy flask-login

所以......我正在尝试从flask app中提交一些sql,模型代码如下:

class User(UserMixin, db.Model):
    __tablename__ = '_users'

    id = db.Column(db.Integer, primary_key=True)
    user_email = db.Column(db.VARCHAR(50), nullable=False, unique=True)
    user_reg_date = db.Column(db.TIMESTAMP, nullable=False)
    last_login = db.Column(db.TIMESTAMP, nullable=True)
    passwd = db.Column(db.VARCHAR(80), nullable=True)
    social_id = db.Column(db.VARCHAR(80), nullable=True, unique=True)

    def __init__(self, user_email, passwd, social_id):
        self.user_email = user_email
        self.user_reg_date = current_time()
        self.passwd = passwd
        self.social_id = social_id


class Player(db.Model):
    __tablename__ = '_players'

    id = db.Column(db.Integer, primary_key=True)
    player_unique_id = db.Column(db.Integer, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('_users.id'))
    affiliated_crew_id = db.Column(db.Integer, db.ForeignKey('crew.id'))
    player_nick = db.Column(db.VARCHAR(50), unique=True)
    player_highscore = db.Column(db.Integer)
    player_badge = db.Column(db.VARCHAR(100))
    player_rank = db.Column(db.Integer)

    def __init__(self, player_unique_id, user_id, affiliated_crew_id
                 , player_nick):
        self.player_unique_id = player_unique_id
        self.user_id = user_id
        self.affiliated_crew_id = affiliated_crew_id
        self.player_nick = player_nick
        self.player_highscore = 0
        self.player_badge = None
        self.player_rank = 0

我已经在SQL中有了正确的列(因为我从预制表中写了这些列),这一切都是正确的。

提交sql的部分如下:

player = Player(player_unique_id=00000, user_id=user_num, affiliated_crew_id=crew_id
                , player_nick=nick)
db.session.add(player)
db.session.commit()

它正在返回:

  

sqlalchemy.exc.InternalError:(pymysql.err.InternalError)(1054,“字段列表中的未知列'_users.id'”)[SQL:'INSERT INTO _players(player_unique_id,user_id,affiliated_crew_id,player_nick, player_highscore,player_badge,player_rank)VALUES(%(player_unique_id)s,_users.id,%(affiliated_crew_id)s,%(player_nick)s,%(player_highscore)s,%(player_badge)s,%(player_rank)s)'] [参数:{'player_unique_id':84658,'affiliated_crew_id':'1','player_nick':'玩家','player_highscore':0,'player_badge':无,'player_rank':0}]

我在这里做错了什么?到目前为止搜索没有帮助...

1 个答案:

答案 0 :(得分:0)

我使用原始的User类来获取User.id,它一直返回None。

由于User是我的flask-login用户类,因此我必须使用current_user.id来提取用户ID。

所以修复user_num初始化user_num = User.id user_num = current_user.id修复了所有内容...

谢谢所有调查问题的人......