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

I am trying to configure flask SQLAlchemy with MySQLWorkBench, The following command works good:

flask db migrate -m'Config'

But when I try to execute flask db upgrade, I get following error:

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

What is the solution for this?

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)


Solution 1:[1]

The error is in the

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

line.

You have not provided a data type for the column date. You should change it to:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vinzee