简体   繁体   English

Python - TypeError:需要一个整数

[英]Python - TypeError: an integer is required

I'm trying to create a database to hold users and climbs with a one-to-many relationship from users to climbs a user has completed. 我正在尝试创建一个数据库来保持用户和攀登者与用户之间的一对多关系,以及用户完成的攀爬。

class Climb(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.DateTime)
    name = db.Column(db.String(64))
    flash = db.Column(db.Boolean)
    notes = db.Column(db.Text)
    type_of_climb = db.Column(db.String(32))
    difficulty = db.Column(db.String(5))
    location = db.Column(db.String(64))
    favorite = db.Column(db.Boolean)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __init__(self, climb_date, name, flash, notes, type_of_climb, difficulty, location, favorite, user_id):
        set_date = datetime.strptime(climb_date, '%Y%m%d').date()
        self.date = set_date
        self.name = name
        self.flash = flash
        self.notes = notes
        self.type_of_climb = type_of_climb
        self.difficulty = difficulty
        self.location = location
        self.favorite = favorite
        self.user_id = user_id

I am trying to test the database using the Python console in PyCharm. 我试图在PyCharm中使用Python控制台测试数据库。 I have successfully created a user and am trying to add a climb to the database but keep getting "TypeError: an integer is required" when I use the following set of commands: 我已经成功创建了一个用户并尝试向数据库添加爬升但是在使用以下命令集时不断收到“TypeError:需要一个整数”:

>>> from flask import Flask
>>> from app import app, db
>>> db.create_all()
>>> from app import User, Climb
>>> c = Climb('2015-05-02', 'Climb so high', 1, 'Made it happen', 'Trad', '5.8', 'Nepal', 0, 1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<string>", line 4, in __init__
  File "/home/logan/Desktop/DEV/git/Climb-On/climb-on-api/flask/local/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 269, in _initialize_instance
    return manager.original_init(*mixed[1:], **kwargs)
  File "/home/logan/Desktop/DEV/git/Climb-On/climb-on-api/app.py", line 50, in __init__
    self.flash = flash
TypeError: an integer is required

I'm totally confused as to how I'm getting that error (flash is the third parameter passed and by my account 1 is an integer). 我对如何得到错误感到困惑(flash是传递的第三个参数,我的帐号1是一个整数)。

Any help is much appreciated. 任何帮助深表感谢。

DBAlchemy可能需要DB中相应表的列不是正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM