Flask-Web-App-Tutorial icon indicating copy to clipboard operation
Flask-Web-App-Tutorial copied to clipboard

SQLite DateTime type only accepts Python datetime and date objects as input.

Open truongluan1202 opened this issue 3 years ago • 1 comments

sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input. i got this error about my DateTime function was not working

This is my code use it :

data = db.Column(db.DateTime(timezone=True), default=func.now()) Can anyone fix this ? I tried use from datetime import datetime but it didn't work.

truongluan1202 avatar Feb 25 '22 19:02 truongluan1202

Do you have the following at the top of your models.py: from sqlalchemy.sql import func

If you do and it still isn't working make sure you installed sqlalchemy like he did at the beginning of the video. Below is my code for models.py if it helps:

from . import db from flask_login import UserMixin from sqlalchemy.sql import func

class Note(db.Model): id = db.Column(db.Integer, primary_key=True) data = db.Column(db.String(10000)) date = db.Column(db.DateTime(timezone=True), default=func.now()) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(150), unique=True) password = db.Column(db.String(150)) first_name = db.Column(db.String(150)) notes = db.relationship('Note')

RenegadeActual avatar Mar 04 '22 03:03 RenegadeActual