Я развернул этот код на heroku, я использую CI / CD с travis и heroku. Для разработки я использую базу данных SQLite. Я хочу, чтобы каждый раз, когда я развертывал репо на heroku, база данных удалялась и создавалась заново с файлом данных. Я сделал это, но heroku дает мне ошибку названия.
Это мой код:
init .py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
import os
db = SQLAlchemy()
def create_app(conf_test=None):
app = Flask(__name__, instance_relative_config=True)
print("instance path --- " + os.path.join(app.instance_path, 'database.db'))
app.config.from_mapping(
SQLALCHEMY_DATABASE_URI = ''.join(['sqlite:////', os.path.join(app.instance_path, 'database.db')]),
SQLALCHEMY_TRACK_MODIFICATIONS = os.environ['SQLALCHEMY_TRACK_MODIFICATIONS'],
JWT_KEY = os.environ['JWT_KEY'],
SECRET_KEY = os.environ['SECRET_KEY']
)
if conf_test is not None:
app.config.update(conf_test)
from .model import init_db
with app.app_context(): init_db()
from .views import init_views
with app.app_context(): init_views()
@app.route("/")
def hello():
return "it's working..."
return app
model.py
class CurrentState(enum.Enum):
available = 'AVAILABLE'
in_progress = 'IN PROGRESS'
finished = 'FINISHED'
thesis_have_keywords = db.Table('thesis_have_keywords',
db.Column('thesis_id', db.Integer, db.ForeignKey("thesis.id")),
db.Column('keyword_id', db.Integer, db.ForeignKey("keyword.id"))
)
thesis_have_teaching = db.Table('thesis_have_teaching',
db.Column('thesis_id', db.Integer, db.ForeignKey("thesis.id")),
db.Column('teaching_id', db.Integer, db.ForeignKey("teaching.id"))
)
thesis_have_courses = db.Table('thesis_have_courses',
db.Column('thesis_id', db.Integer, db.ForeignKey("thesis.id")),
db.Column('course_id', db.Integer, db.ForeignKey("degree_courses.id"))
)
class Teacher(db.Model):
name = db.Column(db.String(50), unique=False, nullable=False)
surname = db.Column(db.String(50), unique=False, nullable=False)
mail = db.Column(db.String(100), unique=True, nullable=False)
id = db.Column(db.String(100), unique=True, nullable=False, primary_key=True)
thesis = db.relationship("Thesis", back_populates="teacher")
class Thesis(db.Model):
title = db.Column(db.String(150), unique=True, nullable=False)
description = db.Column(db.Text, unique=False, nullable=False)
last_modify = db.Column(db.DateTime(timezone=True), unique=False, nullable=False, default=datetime.datetime.utcnow)
duration = db.Column(db.Integer, unique=False, nullable=True)
start_time = db.Column(db.DateTime(timezone=True), unique=False, nullable=True)
end_time = db.Column(db.DateTime(timezone=True), unique=False, nullable=True)
current_state = db.Column(db.Enum(CurrentState), unique=False, nullable=False, default=CurrentState('AVAILABLE'))
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
teacher_id = db.Column(db.Integer, db.ForeignKey("teacher.id"))
teacher = db.relationship("Teacher", back_populates="thesis")
keywords = db.relationship("Keyword", back_populates="thesis", secondary=thesis_have_keywords)
teaching = db.relationship("Teaching", back_populates="thesis", secondary=thesis_have_teaching)
courses = db.relationship("DegreeCourses", back_populates="thesis", secondary=thesis_have_courses)
class Keyword(db.Model):
name = db.Column(db.String(50), unique=True, nullable=False)
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
thesis = db.relationship("Thesis", back_populates="keywords", secondary=thesis_have_keywords)
class Teaching(db.Model):
name = db.Column(db.String(50), unique=False, nullable=False)
code = db.Column(db.String(100), unique=True, nullable=False)
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
thesis = db.relationship("Thesis", back_populates="teaching", secondary=thesis_have_teaching)
class DegreeCourses(db.Model):
name = db.Column(db.String(50), unique=False, nullable=False)
code = db.Column(db.String(100), unique=True, nullable=False)
_type = db.Column(db.String(20), unique=False, nullable=False)
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
thesis = db.relationship("Thesis", back_populates="courses", secondary=thesis_have_courses)
def init_db():
with current_app.app_context():
if os.path.exists(current_app.instance_path):
shutil.rmtree(current_app.instance_path)
try:
os.makedirs(current_app.instance_path)
except OSError:
pass
db.init_app(current_app)
db.drop_all()
db.session.commit()
db.create_all()
with open("server/teachers.json") as teach_file:
data = json.load(teach_file)
teacher_entries=[]
for teacher in data['teachers']:
teach = Teacher(name=teacher['name'], surname=teacher['surname'], mail=teacher['mail'], id=teacher['id'])
teacher_entries.append(teach)
db.session.add_all(teacher_entries)
db.session.commit()
Здесь каждый раз, когда я удаляю папку экземпляра и воссоздаю ее. Я также пытался удалить только файл базы данных, но появляется та же ошибка.
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) таблица thesis_have_keywords уже существует