Flask - SQLAlchemy - «Не удалось открыть указанную таблицу» - PullRequest
0 голосов
/ 07 августа

Сейчас я пытаюсь использовать следующую команду: flask db upgrade (применение процесса миграции) в MySQL. Однако я постоянно сталкиваюсь со следующей ошибкой:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1824, "Failed to open the referenced table 'subjects'")
[SQL: 
CREATE TABLE courses (
        course_id INTEGER NOT NULL AUTO_INCREMENT, 
        course_name VARCHAR(50), 
        course_description VARCHAR(200), 
        other_details VARCHAR(150), 
        admin_id INTEGER, 
        subject_id INTEGER, 
        PRIMARY KEY (course_id), 
        FOREIGN KEY(admin_id) REFERENCES admins (admin_id), 
        FOREIGN KEY(subject_id) REFERENCES subjects (subject_id)
)

Я не понимаю, так как я создал таблицы, насколько мне известно. Все FOREIGN KEYS ссылаются на созданные таблицы.

Следующий код представляет мой models.py класс.


# Third-party imports.
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

# Local imports.
from app import db, login_manager

class User(UserMixin, db.Model):
    __tablename__ = 'users'

    user_id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(30), index=True)
    last_name = db.Column(db.String(30), index=True)
    level_of_education = db.Column(db.String(20), index=True)
    username = db.Column(db.String(50), index=True, unique=True)
    country = db.Column(db.String(30), index=True)
    email = db.Column(db.String(50), index=True, unique=True)
    password_hash = db.Column(db.String(50))
    other_details = db.Column(db.String(100), index=True)

    courses = db.relationship('Course', backref='user', lazy='dynamic')

    # The following will prevent the password being accessed.
    @property
    def password(self):
        raise AttributeError('Given password is not readable.')

    # Setting password to a hashed password instead of readable text.
    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<User: {}'.format(self.username)

@login_manager.user_loader
def load_user(user_id):
    return User.get(int(user_id))

class Admin_and_Author(db.Model):
    __tablename__ = 'admins'

    admin_id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(30), index=True)
    last_name = db.Column(db.String(30), index=True)
    username = db.Column(db.String(50), index=True, unique=True)
    country = db.Column(db.String(30), index=True)
    email = db.Column(db.String(50), index=True, unique=True)
    password_hash = db.Column(db.String(50))
    other_details = db.Column(db.String(100), index=True)
    is_admin = db.Column(db.Boolean, default=False)

    courses = db.relationship('Course', backref='admin_and_author', lazy='dynamic')

        # The following will prevent the password being accessed.
    @property
    def password(self):
        raise AttributeError('Given password is not readable.')

    # Setting password to a hashed password instead of readable text.
    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<Admin: {}'.format(self.username)

@login_manager.user_loader
def load_user(user_id):
    return Admin_and_Author.get(int(admin_id))

class Course(db.Model):
    __tablename__ = 'courses'

    course_id = db.Column(db.Integer, primary_key=True)
    course_name = db.Column(db.String(50), index=True)
    course_description = db.Column(db.String(200))
    other_details = db.Column(db.String(150))
    admin_id = db.Column(db.Integer, db.ForeignKey('admins.admin_id'))
    subject_id = db.Column(db.Integer, db.ForeignKey('subjects.subject_id'))

    subjects = db.relationship('Subject', backref='course', lazy='dynamic')

    def __repr__(self):
        return '<Course: {}'.format(self.course_name)

class Subject(db.Model):
    __tablename__ = 'subjects'

    subject_id = db.Column(db.Integer, primary_key=True)
    subject_name = db.Column(db.String(50), index=True)
    subject_description = db.Column(db.String(200))
    other_details = db.Column(db.String(150))
    admin_id = db.Column(db.Integer, db.ForeignKey('admins.admin_id'))
    course_id = db.Column(db.Integer, db.ForeignKey('courses.course_id'))
    topic_id = db.Column(db.Integer, db.ForeignKey('topics.topic_id'))

    topics = db.relationship('Topic', backref='subject', lazy='dynamic')


    def __repr__(self):
        return '<Subjects: {}'.format(self.subject_name)

class Topic(db.Model):
    __tablename__ = 'topics'

    topic_id = db.Column(db.Integer, primary_key=True)
    topic_name = db.Column(db.String(50), index=True)
    topic_description = db.Column(db.String(200))
    other_details = db.Column(db.String(150))
    admin_id = db.Column(db.Integer, db.ForeignKey('admins.admin_id'))
    course_id = db.Column(db.Integer, db.ForeignKey('courses.course_id'))
    subject_id = db.Column(db.Integer, db.ForeignKey('subjects.subject_id'))


    def __repr__(self):
        return '<Topics: {}'.format(self.topic_name)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...