Python 接收';类型错误';尝试使用SQLAlchemy导入和创建数据库时

Python 接收';类型错误';尝试使用SQLAlchemy导入和创建数据库时,python,flask,orm,sqlalchemy,Python,Flask,Orm,Sqlalchemy,我已经开始了一个项目的工作,并在深入到无法整理ORM之前做出了决定(这意味着要回去重新编写我的类)。我相信我的代码是正确的,但每当我尝试使用Python创建数据库模式时,都会遇到“TypeError”。我在项目目录中加载Python控制台,并键入“from app import db”,这很好。在命令“db.create_all()”(使用基本示例工作)之后,我被抛出错误: "档案" “C:\Users\owner\AppData\Local\Programs\Python38-32\lib\s

我已经开始了一个项目的工作,并在深入到无法整理ORM之前做出了决定(这意味着要回去重新编写我的类)。我相信我的代码是正确的,但每当我尝试使用Python创建数据库模式时,都会遇到“TypeError”。我在项目目录中加载Python控制台,并键入“from app import db”,这很好。在命令“db.create_all()”(使用基本示例工作)之后,我被抛出错误:

"档案" “C:\Users\owner\AppData\Local\Programs\Python38-32\lib\site packages\sqlalchemy\dialogs\mysql\base.py”, 2016年第二行,访问_VARCHAR 返回self.\u extend\u string(type,{},“VARCHAR(%d)”%type.length)类型错误:%d格式:需要数字,而不是类型'

我不知道在我的实际代码中原因可能在哪里,我完全不知所措

任何帮助都会很好-很多代码都被注释掉了,还有一些代码暂时保留了下来,因为我正在添加我的ORM,但它最初是在没有SQLAlchemy的情况下实现的。下面是我的“init.py”代码:

from flask import Flask, render_template, url_for, flash, redirect, g, request, session, send_from_directory, send_file
from heychef.config import Config
from flask_sqlalchemy import SQLAlchemy
import os
import bcrypt
import uuid
import json
import ssl
from datetime import datetime
from datetime import timedelta
from heychef.models.Agency import Agency
from heychef.models.User import User
from heychef.models.Shift import Shift
from flaskext.mysql import MySQL


app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)

from heychef.models.Agent import Agent

@app.before_request
def before_request():
    g.agent = None
    g.agency = None
    if 'agent' in session:
        g.agent = session['agent']
    if 'agency' in session:
        g.agency = session['agency']

##########################################################
#############         Agent Routes        ################
##########################################################

@app.route("/")
def home():
    return render_template('agent-views/signup.html'), 200

@app.route("/agent-signup", methods = ['POST', 'GET'])
def agentSignup():
    if request.method == 'POST':    
        email = request.form['inputEmail']
        firstName = request.form['inputFirstName']
        secondName = request.form['inputSecondName']
        password = request.form['inputPassword']
        rPassword = request.form['inputConfirmPassword']     
        if(password != rPassword):
            flash('Passwords do not match.', 'danger')
            return render_template('agent-views/signup.html')
        else:
            hashedPwd = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
            agent = Agent(firstName, secondName, email, hashedPwd)
            agent.addAgent(self)
            flash('Account created, you can log in now.', 'success')
            return render_template('agent-views/signup.html')

@app.route("/agent-login", methods = ['POST','GET'])
def agentLogin():
    if request.method == 'POST':
        session.pop('agent', None)
        email = request.form['inputEmail']
        pwd = request.form['inputPassword']
        connection = mysql.get_db()
        cursor = connection.cursor()
        result, msg = Agent.agentLogin(connection, cursor, email, pwd)
        if(result):
            session['agent'] = email
            flash(msg, 'success')
            return redirect(url_for('agentDashboard'))
        else:       
            flash(msg, 'danger')
            return render_template('agent-views/login.html')
    else:
        return render_template('agent-views/login.html')

@app.route("/agent-dashboard", methods = ['GET'])
def agentDashboard():
    if g.agent:
        return render_template('agent-views/dashboard.html'), 200
    else:
        msg = "Please log in"
        flash(msg, 'warning')
        return redirect(url_for('agentLogin'))

if __name__ == "__main__":
    app.run()
这是我的代理课程:

from heychef.models.User import User
#from heychef.models.data.AgentDb import AgentDb
from heychef import db

class Agent(db.Model):
    agentId = db.Column(db.Integer, unique=True, nullable=False, primary_key=True)
    firstName = db.Column(db.String(80), unique=False, nullable=False, primary_key=False)
    secondName = db.Column(db.String(80), unique=False, nullable=False, primary_key=False)
    email = db.Column(db.String(80), unique=False, nullable=False, primary_key=False)
    level = db.Column(db.String(80), unique=False, nullable=True, primary_key=False)
    agencyId = db.Column(db.Integer, unique=False, nullable=True, primary_key=False)
    addressLine1 = db.Column(db.String(80), unique=False, nullable=True, primary_key=False)
    addressLine2 = db.Column(db.String(80), unique=False, nullable=True, primary_key=False)
    addressLine3 = db.Column(db.String(80), unique=False, nullable=True, primary_key=False)
    postcode = db.Column(db.String(20), unique=False, nullable=True, primary_key=False)
    passwordHash = db.Column(db.String(256), unique=False, nullable=False, primary_key=False)

    def __repr__(self):
        return "<Agent(firstName='%s', lastName='%s')>" % (self.firstName, self.lastName)

    def __init__(self, firstName, secondName, email, hashedPwd):
        self.agentId = 1001
        self.firstName = firstName
        self.secondName = secondName
        self.email = email
        self.passwordHash = hashedPwd


    @staticmethod
    def agentEmailExists(cursor, email):
        exists = AgentDb.agentEmailExistsDb(cursor, email)
        return exists

    @staticmethod
    def addAgent(agent):
        db.session.add(self)
        db.session.commit()

    @staticmethod
    def agentLogin(connection, cursor, email, pwd):
        failMsg = 'Email or Password does not match.' 
        successMsg = 'Successfully logged in.'
        result = AgentDb.agentLoginDb(connection, cursor, email, pwd)
        if(result):
            msg = successMsg
        else:
            msg = failMsg
        return result, msg

    @staticmethod
    def getWork(cursor):
        work = AgentDb.getWorkDb(cursor)
        return work
来自heychef.models.User导入用户
#从heychef.models.data.AgentDb导入AgentDb
从heychef导入数据库
类代理(db.Model):
agentId=db.Column(db.Integer,unique=True,nullable=False,primary_key=True)
firstName=db.Column(db.String(80),unique=False,nullable=False,primary_key=False)
secondName=db.Column(db.String(80),unique=False,nullable=False,primary_key=False)
email=db.Column(db.String(80),unique=False,nullable=False,primary\u key=False)
level=db.Column(db.String(80),unique=False,nullable=True,primary\u key=False)
agencyId=db.Column(db.Integer,unique=False,nullable=True,primary\u key=False)
addressLine1=db.Column(db.String(80),unique=False,nullable=True,primary_key=False)
addressLine2=db.Column(db.String(80),unique=False,nullable=True,primary_key=False)
addressLine3=db.Column(db.String(80),unique=False,nullable=True,primary_key=False)
postcode=db.Column(db.String(20),unique=False,nullable=True,primary\u key=False)
passwordHash=db.Column(db.String(256),unique=False,nullable=False,primary_key=False)
定义报告(自我):
返回“”%(self.firstName,self.lastName)
定义初始化(self、firstName、secondName、email、hashedPwd):
self.agentId=1001
self.firstName=firstName
self.secondName=secondName
self.email=电子邮件
self.passwordHash=hashedPwd
@静力学方法
def AgentMailExists(光标、电子邮件):
exists=AgentDb.agentEmailExistsDb(光标,电子邮件)
回报存在
@静力学方法
def添加代理(代理):
db.session.add(self)
db.session.commit()
@静力学方法
def代理登录(连接、光标、电子邮件、pwd):
failMsg='电子邮件或密码不匹配。'
Successsg='已成功登录。'
结果=AgentDb.agentLoginDb(连接、光标、电子邮件、pwd)
若有(结果):
msg=successsg
其他:
msg=failMsg
返回结果,msg
@静力学方法
def getWork(光标):
work=AgentDb.getWorkDb(游标)
复工
任何帮助都是王牌,因为我真的很挣扎


非常感谢

该错误似乎与VARCHAR(String)db列有关,并指出它收到了一个非整数值,因此在项目中的其他模型类中可能有一个带有非整数值或空括号的
db.String()
model字段。

我看到的一个问题(可能无关):
self.secondName
在你的init中与
self.lastName
在你的reprHi中,我更改了它,很遗憾它是不相关的:-(但感谢你指出!!:-)碰巧这是相关的-我以前只更改了lastName的第一个实例。谢谢我做了这么多,我只是努力想知道它指的是什么,但现在它已经解决了,非常感谢!:-)