【问题标题】:AttributeError: module 'sqlalchemy' has no attribute 'NullType'AttributeError:模块“sqlalchemy”没有属性“NullType”
【发布时间】:2021-09-09 07:49:12
【问题描述】:

我正在尝试使用 MySQLWorkBench 配置烧瓶 SQLAlchemy,以下命令效果很好。
flask db 迁移 -m'Config'
但是当我尝试执行烧瓶数据库升级时 我收到以下错误。

AttributeError: module 'sqlalchemy' has no attribute 'NullType'

解决办法是什么?

app.py:

from flask import Flask , render_template ,redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password!@localhost/blog'

# initialize
db = SQLAlchemy(app)
migrate = Migrate(app, db)

# To Db
class PostTable(db.Model):
    sno = db.Column(db.Integer, primary_key=True, unique=True, nullable=False)
    date = db.Column(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), nullable=False)
    name = db.Column(db.String(120), unique=False, nullable=False)
    emailid = db.Column(db.String(50), unique=False, nullable=False)
    phone = db.Column(db.String(15), unique=False, nullable=False)
    message = db.Column(db.String(120), unique=False, nullable=False)

@app.route("/")
def home():
    return render_template('index.html')

@app.route("/homeclick")
def homeclick():
    return render_template('index.html')

@app.route("/about")
def about():
    return render_template('about.html')

@app.route("/post")
def post():
    return render_template('post.html')

@app.route("/contact" , methods=['POST', 'GET'])
def contact():
    if request.method == 'POST' :
        # add entry to the database

        name = request.form.get('name')
        emailid = request.form.get('email')
        phone = request.form.get('phone_number')
        message = request.form.get('message')

        entry_to_db = PostTable(name=name, emailid=emailid , phone=phone,message=message)
        db.session.add(entry_to_db)
        db.session.commit()
        return render_template('contact.html')

       if __name__ == '__main__':
       app.run(debug=True, port=4013 , threaded=True)

【问题讨论】:

  • edit问题包含完成错误回溯。

标签: python mysql flask sqlalchemy flask-sqlalchemy


【解决方案1】:

错误在

date = db.Column(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), nullable=False)

行。

您尚未提供日期列的数据类型。您应该将其更改为:

date = db.Column(db.DateTime, nullable=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2019-07-20
    • 2021-11-05
    相关资源
    最近更新 更多