I m new to Flask. I'm trying to run flask db upgrade
but every time I encounter an error sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1005, "Can't create table 'incentive_db.employees' (errno: 150)")
. I have tried searching through, but I have not found a specific answer.
Here is my models.py code:
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)
What could I be doing wrong? Here is the full error text:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1005, "Can't create table 'incentive_db.employees' (errno: 150)") [SQL: u'\nCREATE TABLE employees (\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) REFERENCES projects (id), \n\tFOREIGN KEY(role_id) REFERENCES roles (id), \n\tCHECK (is_admin IN (0, 1))\n)\n\n']