简体   繁体   English

在Flask SQLAlchemy中创建数据库

[英]Creating a database in flask sqlalchemy

I'm building a Flask app with Flask-SQLAlchemy and I'm trying to write a script that will create a Sqlite3 database without running the main application. 我正在用Flask-SQLAlchemy构建一个Flask应用程序,并且试图编写一个无需运行主应用程序即可创建Sqlite3数据库的脚本。 In order to avoid circular references, I've initialized the main Flask app object and the SQLAlchemy database object in separate modules. 为了避免循环引用,我在单独的模块中初始化了Flask应用程序主对象和SQLAlchemy数据库对象。 I then import and combine them in a third file when running the app. 然后,在运行该应用程序时,我将其导入并合并到第三个文件中。 This works fine when I'm running the app, as the database is built and operates properly when create rows and query them. 当我运行应用程序时,这可以很好地工作,因为数据库已构建,并且在创建行和查询行时可以正常运行。 However, when I try to import them in another module, I get the following error: 但是,当我尝试将它们导入另一个模块时,出现以下错误:

RuntimeError: application not registered on db instance and no applicationbound to current context

My code looks like the following: 我的代码如下所示:

root/create_database.py root / create_database.py

from application.database import db
from application.server import app

db.init_app(app)

db.create_all()

root/run.sh 根/run.sh

export FLASK_APP=application/server.py
flask run

root/application/ init .py root /应用程序/ init .py

from database import db
from server import app

db.init_app(app)    

from routes import apply_routes   
apply_routes(app)

root/application/database.py 根目录/应用程序/数据库.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

root/application/server.py 根/应用程序/ server.py

from flask import Flask
import os
app = Flask(__name__)

path = os.path.dirname( os.path.realpath(__file__) )
database_path = os.path.join(path, '../mydb.sqlite')

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + database_path

root/application/models/ init .py root /应用程序/模型/ init .py

from user import User

root/application/models/user.py 根目录/应用程序/模型/user.py

from application.database import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(120))

    def __init__(self, username, password):
        self.username = username
        self.password = password

In my create_database.py script I'm trying to make sure that the SQLAlchemy db instance is configured with the config details from the app object, but it doesn't seem to be connecting for some reason. 在我的create_database.py脚本中,我试图确保已使用来自app对象的配置详细信息配置了SQLAlchemy db实例,但是由于某种原因,它似乎没有连接。 Am I missing something important here? 我在这里错过了重要的事情吗?

You either have to create a request or you have to create the models with sqlalchemy directly. 您要么必须创建一个请求,要么必须直接使用sqlalchemy创建模型。 We do something similar at work and chose the former. 我们在工作中做了类似的事情,选择了前者。

Flask lets you create a test request to initialize an app. Flask允许您创建测试请求以初始化应用程序。 Try something like 尝试类似

from application.database import db
from application.server import app

with app.test_request_context():
     db.init_app(app)

     db.create_all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM