flask_SQLAlChemy create_all()不起作用

2022-03-10 00:00:00 python sqlalchemy flask flask-sqlalchemy

问题描述

我开始学习如何对我的代码使用SQLAlChemy,但由于某种原因,当我运行代码时,它引发了此异常:

Traceback (most recent call last):
  File "C:/Users/smadar.KLAG/PycharmProjects/keylogger/site/practice.py", line 118, in <module>
    db.create_all()
  File "C:Userscprojects	estvenvlibsite-packagesflask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:Userscprojects	estvenvlibsite-packagesflask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:Userscprojects	estvenvlibsite-packagesflask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:Userscprojects	estvenvlibsite-packagesflask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:Userscprojects	estvenvlibsite-packagesflask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:Userscprojects	estvenvlibsite-packagesflask_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

代码如下:


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)

我尝试运行我的一个朋友的代码,他有SQLAlChemy(该代码可以为他工作),以防是编码问题,但我也发现他的代码出现了同样的错误。
我是否做错了什么?


解决方案

我也是刚碰到这个。看起来SQLAlChemy刚刚发布了1.4版,该版本打破了flask_sqlalChemy。

我能够通过安装以前的(1.3)版本在我的系统上解决此问题。

相关文章