简体   繁体   中英

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. 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:

>>> 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).

Any help is much appreciated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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