Flask_SQLAlchemy create_all() doesn't work

Viewed 523

I started learning how to use SQLAlchemy for my code but for some reason when I ran the code it raised this exception:

Traceback (most recent call last):
  File "C:/Users/smadar.KLAG/PycharmProjects/keylogger/site/practice.py", line 118, in <module>
    db.create_all()
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:\Users\c\projects\test\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 914, in apply_driver_hacks
    sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

This is the code:


from flask import Flask, redirect, url_for, render_template, request, session, flash
from time import sleep
from flask_sqlalchemy import SQLAlchemy

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

class Users(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    email = db.Column(db.String(100))
    password = db.Column(db.String(100))



if __name__ == '__main__':
    db.create_all()
    app.run(debug=True)

I tried running the code of a friend of mine that has SQLAlchemy (the code does work for him) incase it was a coding issue but I have reached the same error with his code as well.
Is there something I am doing incorrectly?

2 Answers

I just ran into this too. Looks like SQLAlchemy just released version 1.4, which breaks flask_sqlalchemy.

I was able to resolve the issue on my system by installing the previous (1.3) version instead.

EDIT: the latest version of SQLAlchemy works now (version 1.4 as of writing this)

installing SQLAlchemy 1.3.23 worked for me.

pip install SQLAlchemy==1.3.23
Related