2

I try to create a database and put an article table in routes.py's /articles/<article_name> route but it gives me AttributeError: can't set attribute. I looked up other sources for a solution but they seemed irrelevant. Error occurs when create_all(), add(), commit() are used.

models.py

from files import db
from datetime import datetime

class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False,unique=True)
    article_content = db.Column(db.Text)
    date = db.Column(db.DateTime,default=datetime.utcnow)
    abstract = db.Column(db.String(150))
    comments = db.relationship("Comment", backref="article", lazy = True)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(db.String(30), unique=True, nullable=False)
    password = db.Column(db.String(30), nullable=False)
    comments = db.relationship("Comment", backref="user", lazy = True)

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_comment = db.Column(db.String(200), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
    article_id = db.Column(db.Integer, db.ForeignKey("article.id"))

init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SECRET_KEY'] = ''
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)

from files import routes

routes.py

from files import app, db
from flask import render_template
from files.models import Article
from files.forms import RegisterForm, LoginForm

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

@app.route('/articles/<article_name>') # dynamic route
def article(article_name):
    db.create_all()

    article_ = Article(title = article_name, article_content = "article", abstract = "ndng")

    db.session.add(article_)
    db.session.commit()
    return render_template("article.html", article_name=article_name)

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

@app.route('/register')
def register():
    form = RegisterForm()
    return render_template("register.html", form = form)


@app.route('/login')
def login():
    form = LoginForm()
    return render_template("login.html", form = form)
aras edeş
  • 53
  • 1
  • 7
  • 1
    Can you provide the whole error message? With location and info about what threw the error. – Saintan Mar 16 '21 at 22:41
  • Agreed w/ @Saintan. You should edit the question to include the message, as opposed to putting it in a comment. Also, it doesn't seem like a good idea to do `db.create_all()` in the route. That basically means you're trying to re-create the tables every time someone hits that route. You can put that line into `__init__.py` instead, it seems like. – mechanical_meat Mar 16 '21 at 22:43
  • I was trying to see if it works before proceeding. – aras edeş Mar 17 '21 at 06:57

3 Answers3

3

I recently delt with that same error message. It is actually due to an upgrade in SQLAlchemy, which gets installed as a dependency of flask-sqlalchemy.

You can find the question I posted on stackoverflow for the issue here:

The problem gets solved by uninstalling SQLAlchemy==1.4.0 and installing the previous version SQLAlchemy==1.3.23.

Try doing that and see if it helps.

Ange Uwase
  • 561
  • 4
  • 10
2

Just Downgrade the SQLAlchemy . For some reason the latest version is not working

pip install 'SQLAlchemy<1.4.0'
Naheed
  • 61
  • 7
1

Downgrading SQLAlchemy to anything lower than 1.4.0 (1.3.8 in my case) solves the problem.

aras edeş
  • 53
  • 1
  • 7