Я использую Flask
как основу для создания приложения. В настоящее время я пытаюсь реализовать следующий файл models.py
:
# The purpose for this class is to represent the applications
# database table in code.
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
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)
Однако всякий раз, когда в терминал вводятся следующие команды: flask db migrate
и flask db upgrade
, возникает следующая ошибка:
sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1050 (42S01): Table 'admins' already exists
Я проверил MySQL и добавляется только таблица Admins
. Остальные были проигнорированы. Я полностью озадачен тем, как это исправить, даже после длительного поиска в Google.
Есть какой-нибудь совет?