2

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?

shnap
  • 175
  • 1
  • 1
  • 10
  • Have you compared your version of modules vs your friends? Same with Python – CodeLikeBeaker Mar 15 '21 at 21:53
  • have the same issue, you're not alone friend – CPunkh Mar 15 '21 at 22:13
  • what version of python are you running? i have the same issue with 3.7 on debian – CPunkh Mar 15 '21 at 22:15
  • 1
    We both used the same exact tutorial one for one about how to establish the database, but for some reason it does work for him while it doesn't work for me (I didn't try to give him my code though). And I am running on 3.7 as well – shnap Mar 15 '21 at 22:23
  • 1
    curiously setting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:" works, so for whatever reason it's failing to make the actual file im assuming, try upgrading to a newer python version? i'll do the same and report back soon – CPunkh Mar 15 '21 at 22:24
  • 1
    That's weird, it also does work for me if I change it to memory although for some reason my friend can still use sqlite:///users.sqlite3. There is a chance it is because he has the database already built but I am not sure. – shnap Mar 15 '21 at 22:31
  • Upgrading to 3.8 didn't solve the issue, I also tried older versions of flask-sqlalchemy, no dice – CPunkh Mar 15 '21 at 22:38
  • 1
    If it is like that I suppose it is a problem on their end, unless something is missing that we don't know of , although this is unlikely as the source of the problem is from setting the SQLALCHEMY_DATABASE_URI to sqlite:///users.sqlite3. Something with the particular thing? – shnap Mar 15 '21 at 22:49

2 Answers2

3

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.

daveterra
  • 46
  • 2
2

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
asbi park
  • 21
  • 2