Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'SQLAlchemy' object has no attribute 'datetime'

Tags:

python

flask

Im following a youtube tutorial on using Flask and when i run this code by using python -m flask run it shows this AttributeError: 'SQLAlchemy' object has no attribute 'datetime'. How can I fix it?

from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy

from datetime import datetime as dt

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.datetime, default=datetime.utcnow)

    def __repr__(self):
        return '<Task %r' % self.id

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

if __name__ == "__main__":
    app.run(debug=True)
like image 939
Wayne Avatar asked May 11 '26 17:05

Wayne


1 Answers

Column types are classes and capitalized. Try this:

db.Column(db.DateTime, default=datetime.utcnow)

Notice DateTime, not datetime.

like image 103
Ken Kinder Avatar answered May 14 '26 08:05

Ken Kinder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!