我希望每次命令migrate时,都能在包文件夹中自动创建app.db



这是我的目录

hotel/
___grandluis/
______main/
______static/
______templates/
______users/
______config.py
______models.py
___migrations/
______versions/
___run.py
___app.db

我的问题是我希望我的app.db在grandluis文件夹中。因为每次我运行CCD_ 2。我的app.db是在grandluis文件夹之外创建的,当我通过我的应用程序向数据库添加内容时,它会在grandluis文件夹内创建另一个app.db。我觉得我的烧瓶应用程序只读取grandluis文件夹内的app.db,而不是grandluis文件夹外的应用程序。因为它可能会导致错误。它说没有创建SQLAlchemy.model。因为我的应用程序只读取grandluis内部的app.db。因此,我希望每次命令migrate时,它都会创建/影响grandluis文件夹中的app.db。我真的想要一些答案,伙计们,非常感谢你们有时间阅读我的这个荒谬的问题。

--型号.py-

from datetime import datetime
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
from grandluis import db, loginManager
from flask_login import UserMixin
@loginManager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
UserId = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(300), nullable=False)
address = db.Column(db.String(100), nullable=False)
contact = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
username = db.Column(db.String(20), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
class Room(db.Model):
roomId = db.Column(db.Integer, primary_key=True)
roomNum = db.Column(db.String(100), nullable=False)
roomType = db.Column(db.String(100), nullable=False)
roomPrice = db.Column(db.Integer, nullable=False)
roomStatus = db.Column(db.String(100), nullable=False)
class Meals(db.Model):
mealId = db.Column(db.Integer, primary_key=True)
mealName = db.Column(db.String(100), nullable=False)
mealType = db.Column(db.String(100), nullable=False)
mealPrice = db.Column(db.String(100), nullable=False)
mealStatus = db.Column(db.String(100), nullable=False)
class Service(db.Model):
serId = db.Column(db.Integer, primary_key=True)
serName = db.Column(db.String(100), nullable=False)
serPrice = db.Column(db.String(100), nullable=False)
serStatus = db.Column(db.String(100), nullable=False)
class Guest(db.Model, UserMixin):
GuestId = db.Column(db.Integer, primary_key=True)
firstName = db.Column(db.String(100), nullable=False)
lastName = db.Column(db.String(100), nullable=False)
middleName = db.Column(db.String(100), nullable=False)
address = db.Column(db.String(100), nullable=False)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
password = db.Column(db.String(60), nullable=False)
#posts = db.relationship('Post', backref='author', lazy=True)
def get_reset_token(self, expires_sec=1800):
s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
return s.dumps({'guest_id': self.id}).decode('utf-8')
@staticmethod
def verify_reset_token(token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
user_id = s.loads(token)['guest_id']
except:
return None
return Guest.query.get(user_id)
def __repr__(self):
return f"Guest('{self.username}', '{self.email}', '{self.image_file}')"

--config.py-

class Config:
SECRET_KEY = 'XXXXXXXXXXX'
SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
MAIL_SERVER = 'smtp.googlemail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = 'xxxxxx@gmail.com'
MAIL_PASSWORD = 'xxxxxx'

--run.py-

from grandluis import db, createApp
from flask_migrate import MigrateCommand
from flask_script import Manager
app = createApp()
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()

我认为问题出在Config.py python文件中。您应该正确地指定数据库的路径。更改您的代码如下:

首先,将操作系统库导入config.py文件。

import os

然后制作一个变量。在我的例子中,我将该变量命名为basedir:

basedir = os.path.abspath(os.path.dirname(__file__))

现在,您可以指定数据库的路径。按如下方式更改SQLALCHEMY_DATABASE_URI:

SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 
'sqlite:///' + os.path.join(basedir, 'hotel\grandluis\app.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False

转到您的run.py python文件并进行以下更改:

from grandluis import db, createApp
from flask_migrate import MigrateCommand
from flask_script import Manager
fro flask_sqlalchemy import SQLAlchemy
app = createApp()
db = SQLAlchemy(app)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
db.init_app(app)
db.app = app
db.create_all()
if __name__ == '__main__':
manager.run()

相关内容

  • 没有找到相关文章

最新更新