烧瓶:无法运行`flask db upgrade`

时间:2017-12-10 05:03:23

标签: python mysql flask flask-sqlalchemy

我是Flask的新手。我正在尝试运行flask db upgrade,但每次遇到错误sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1005, "Can't create table 'incentive_db.employees' (errno: 150)")时。我试过搜索,但我没有找到具体的答案。

这是我的models.py代码:

from flask_login import UserMixin
from werkzeug.security import generate_password_hash, 
check_password_hash

from app import db, login_manager

class Employee(UserMixin, db.Model):
    """
    Create Employee table
    """

    __tablename__ = 'employees'

    id = db.Column(db.Integer, primary_key=True)
    emp_number = db.Column(db.String(10), index=True, unique=True)
    username = db.Column(db.String(60), index=True, unique=True)
    emp_name = db.Column(db.String(100), index=True)
    password_hash = db.Column(db.String(128))
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
    is_admin = db.Column(db.Boolean, default=False)
    subprojects = db.relationship('Subproject', backref='employee', 
                                lazy='dynamic')
    attendances = db.relationship('Attendance', backref='employee', 
                                lazy='dynamic')
    incentives = db.relationship('Incentive', backref='employee', 
                                lazy='dynamic')
    projects = db.relationship('Project', backref='employee', 
                                lazy='dynamic')

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

# Set up user_loader
@login_manager.user_loader
def load_user(user_id):
    return Employee.query.get(int(user_id))

class Incentive(db.Model):
    """
    Create an Incentive table
    """

    __tablename__ = 'incentives'

    id = db.Column(db.Integer, primary_key=True)
    inc_employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
    subproject_id = db.Column(db.Integer, db.ForeignKey('subprojects.id'))
    inc_attendances_id = db.Column(db.Integer, db.ForeignKey('attendances.id'))
    production = db.Column(db.Integer)
    av_qa_score = db.Column(db.Integer)
    total_points = db.Column(db.Integer)
    amount = db.Column(db.Float)


class Project(db.Model):
    """
    Create a Project table
    """

    __tablename__ = 'projects'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), unique=True)
    pro_employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
    description = db.Column(db.String(200))
    employees = db.relationship('Employee', backref='project',
                             lazy='dynamic')
    subprojects = db.relationship('Subproject', backref='project', 
                                lazy='dynamic')

    def __repr__(self):
        return '<Project: {}>'.format(self.name)

class Subproject(db.Model):
    """
    Create a Subproject class
    """

    __tablename__ = 'subprojects'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), unique=True)
    description = db.Column(db.String(200))
    project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
    incentives = db.relationship('Incentive', backref='subproject', 
                                lazy='dynamic')

    def __repr__(self):
    return '<Subproject: {}>'.format(self.name)

class Attendance(db.Model):
    """
    Create an Attendance class
    """

    __tablename__ = 'attendances'

    id = db.Column(db.Integer, primary_key=True)
    att_employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'))
    leave_days = db.Column(db.Integer)
    days_present = db.Column(db.Integer)
    percentage_attendance = db.Column(db.Integer)

我可能做错了什么?这是完整的错误文本:

  

sqlalchemy.exc.OperationalError:(_mysql_exceptions.OperationalError)(1005, “无法创建表 'incentive_db.employees'(错误:150)”)[SQL:U'\ n创建TABLE雇员(\ n \ TID INTEGER NOT NULL AUTO_INCREMENT,\ n \ temp_number VARCHAR(10),\ n \ tusername VARCHAR(60),\ n \ temp_name VARCHAR(100),\ n \ tpassword_hash VARCHAR(128),\ n \ tproject_id INTEGER,\ n \ trole_id INTEGER,\ n \ tis_admin BOOL,\ n \ tPRIMARY KEY(ID),\ n \ tFOREIGN KEY(PROJECT_ID)参考文献项目(ID),\ n \ tFOREIGN KEY(ROLE_ID)参考角色(ID),\ n \ t检查( is_admin IN(0,1))\ n)\ n \ n']

0 个答案:

没有答案