Flask SQLAlchemy db.create_all(app = app) 无法设置属性。没有创建 database.db 文件。每次我尝试创建它都表明它无法设置属性。我在另一个应用程序中使用了相同的技术,但当时它有效。请帮我摆脱这个错误。我是烧瓶的新手。
这是我的init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
# creating the object for database
db = SQLAlchemy()
DB_NAME = "database.db"
# for creating the app
def create_app():
# initializing the app
app = Flask(__name__)
app.config['SECRET_KEY'] = "Any secret key"
# initializing the data base
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
# for debugging sql
# app.config['SQLALCHEMY_ECHO'] = True
db.init_app(app)
# now for registering the blueprints
from .views import views
from .auth import auth
app.register_blueprint(views , url_prefix='/')
app.register_blueprint(auth , url_prefix='/')
# for creating database
# from .models import User , Room , Participant , Message
from .models import User,Note
create_database(app)
return app
def create_database(app):
# if the database does not exist then we will create it
if not path.exists('website/'+DB_NAME):
db.create_all(app = app)
print('Database created')
我的模型.py
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
# The User will inherit database model and UserMixin . The UserMixin is for authentication
# purpose
class User(db.Model , UserMixin):
id = db.Column(db.Integer , primary_key = True)
email = db.Column(db.String(150) , unique = True)
password = db.Column(db.String(150))
firstname = db.Column(db.String(150))
lastname = db.Column(db.String(150))
# a sudo column in Note
notes = db.relationship('Note')
# def __repr__(self):
# print(f"<Post {self.firstname}>")
class Note(db.Model):
id = db.Column(db.Integer , primary_key = True)
data = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default = func.now())
user_id = db.Column(db.Integer , db.ForeignKey('user.id'))
main.py
from website import create_app
app = create_app()
if __name__ == '__main__':
# This is a debugging server
app.run(debug = True)
这是应用程序结构的图片: app structure
日志如下: 在日志中它说无法设置该属性。 我也尝试过手动构建数据库,但它不起作用
(venv) naheed@naheed-s15-x530un:~/Desktop/flask-chat-app-v2$ python main.py
/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:833: 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.
warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
File "main.py", line 3, in <module>
app = create_app()
File "/home/naheed/Desktop/flask-chat-app-v2/website/__init__.py", line 32, in create_app
create_database(app)
File "/home/naheed/Desktop/flask-chat-app-v2/website/__init__.py", line 40, in create_database
db.create_all(app = app)
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine
return connector.get_engine()
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 555, in get_engine
options = self.get_options(sa_url, echo)
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 570, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "/home/naheed/Desktop/flask-chat-app-v2/venv/lib/python3.8/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
答案 0 :(得分:0)
添加以下导入:
from sqlalchemy import create_engine
然后在你的 create_app() 函数中初始化它:
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Any secret key'
# app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
engine = create_engine('sqlite:///{DB_NAME}')
db.init_app(app)
答案 1 :(得分:0)
我已经通过降级 SQLAlchemy 解决了这个问题。由于某种原因,最新版本不起作用
<块引用>pip 安装'SQLAlchemy<1.4.0'