0

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']

James Z
  • 12,209
  • 10
  • 24
  • 44
daktari
  • 91
  • 1
  • 8
  • `flask db init` and `flask db migrate` have run successfully and migrations created. – daktari Dec 10 '17 at 05:15
  • It seems like it's an issue with your foreign key specifications. See https://stackoverflow.com/questions/1457305/mysql-creating-tables-with-foreign-keys-giving-errno-150 – John Gordon Dec 10 '17 at 05:58
  • @JohnGordon all my foreign keys are integers and I have tried making them unique, such that no other table has a foreign key named similarly in another table, that's why I was appending the first table letters to the foreign keys in cases where two tables reference once table. Kindly help me understand. – daktari Dec 10 '17 at 06:34
  • Do the `projects` and `roles` tables exist already? – John Gordon Dec 10 '17 at 16:34
  • Yes, they do exist. I removed them just to reduce my code. – daktari Dec 10 '17 at 18:59
  • Do they have the correct data type for their `id` columns to match the foreign key declarations? – John Gordon Dec 10 '17 at 22:03
  • Did you follow the advice in the question I linked above regarding the `SHOW ENGINE INNODB STATUS` command? – John Gordon Dec 10 '17 at 22:06
  • class Role(db.Model): """ Create a Role table """ __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), unique=True) description = db.Column(db.String(200)) employees = db.relationship('Employee', backref='role', lazy='dynamic') def __repr__(self): return ''.format(self.name) – daktari Dec 11 '17 at 01:27
  • I read through the linked question and all my foreign keys reference primary keys and are type `Int` Sorry about the formatting, I'm unable to find a way of posting code blocks in the comment section. – daktari Dec 11 '17 at 01:32
  • I managed to correct the error. Thanks @JohnGordon for your help. The one-to-many relationships had been messed up. – daktari Dec 11 '17 at 10:33

0 Answers0