Nwankwo Fortune
Nwankwo Fortune

Reputation: 43

sql achemy flask not importing db

I'm trying to import my data base but it keeps throwing an error , I don't know where i am wrong

Here s my code from the main app(flaskblog.py)

from datetime import datetime
from flask import Flask, render_template, url_for, flash, redirect
from flask_sqlalchemy import SQLAlchemy
from forms import RegistrationForm, LoginForm
 
app = Flask(__name__)
app.config['SECRET_KEY'] = 'cac78a5498388aa4a95fb2be0f0a6499'
app.config['SQLALCHEMY_DATABASE_URI'] = 'aqlite:///site.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), unique=True, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref="author", lazy=True)

    def __repr__(self):
        return f"User('{self.username}','{self.email}','{self.image_file}')"

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, datetime=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Post('{self.title}','{self.date_posted}')"

And here is the error I keep receiving:

>>> from flaskblog import db
C:\Users\hp\miniconda3\lib\site-packages\flask_sqlalchemy\__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\hp\Flask_Blog\flaskblog.py", line 22, in <module>
    class Post(db.Model):
  File "C:\Users\hp\Flask_Blog\flaskblog.py", line 25, in Post
    date_posted = db.Column(db.DateTime, nullable=False, datetime=datetime.utcnow)
  File "C:\Users\hp\miniconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 1644, in __init__
    self._extra_kwargs(**kwargs)
  File "C:\Users\hp\miniconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 1686, in _extra_kwargs
    self._validate_dialect_kwargs(kwargs)
  File "C:\Users\hp\miniconda3\lib\site-packages\sqlalchemy\sql\base.py", line 416, in _validate_dialect_kwargs
    "named <dialectname>_<argument>, got '%s'" % k
TypeError: Additional arguments should be named <dialectname>_<argument>, got 'datetime'

This error is quite strange to me , I have looked at the code line by line but still no fixes

Upvotes: 0

Views: 206

Answers (1)

Patrick Yoder
Patrick Yoder

Reputation: 1153

The problem is that Sqlalchemy columns have no attribute 'datetime'. I believe 'default' is the keyword you are looking for. Change this line,

date_posted = db.Column(db.DateTime, nullable=False, datetime=datetime.utcnow)

to this one,

date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

Upvotes: 1

Related Questions