0

I have two models in my flask application User and Employee.

User class:


    from flask_login import UserMixin
    from . import db


    class User(UserMixin, db.Model):
        id = db.Column(db.Integer, primary_key=True)  # primary keys are required in SQLAlchemy
        email = db.Column(db.String(100), unique=True)
        password = db.Column(db.String(100), nullable=False)
        name = db.Column(db.String(1000), nullable=False)
        user_type = db.Column(db.Enum("Candidate", "Employee", name="user_type"))

        def get_id(self):
            return self.id

Employee class:


    from . import db
    from .model_user import User


    class Employee(db.Model):
        employee_id = db.Column(db.Integer, primary_key=True)
        employee_title = db.Column(db.String(100), nullable=False)
        employee_permissions = db.Column(db.String(10000), nullable=True)
        user_id = Column('user_id', Integer, ForeignKey("user.id"))
        user = relationship("User", backref=backref("user", uselist=False))

        def get_id(self):
            return self.employee_id

I tried using the

db.create_all(app=create_app())

where db = SQLAlchemy() and create_app() function returns the flask app

I am getting the following error

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\schema.py", line 926, in create
    bind._run_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 2104, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1663, in _run_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\visitors.py", line 144, in traverse_single
    return meth(obj, **kw)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\ddl.py", line 827, in visit_table
    self.connection.execute(
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1020, in execute
    return meth(self, multiparams, params)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\ddl.py", line 72, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\engine\base.py", line 1071, in _execute_ddl
    compiled = ddl.compile(
  File "<string>", line 1, in <lambda>
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\elements.py", line 476, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\ddl.py", line 29, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 319, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\visitors.py", line 95, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 2914, in visit_create_table
    const = self.create_table_constraints(
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 2963, in create_table_constraints
    return ", \n\t".join(
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 2963, in <genexpr>
    return ", \n\t".join(
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 2966, in <genexpr>
    self.process(constraint)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\visitors.py", line 95, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\compiler.py", line 3214, in visit_foreign_key_constraint
    remote_table = list(constraint.elements)[0].column.table
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\util\langhelpers.py", line 883, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "C:\Users\krish\AppData\Local\Programs\Python\Python38\lib\site-packages\sqlalchemy\sql\schema.py", line 2125, in column
    raise exc.NoReferencedTableError(
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'employee.user_id' could not find table 'User' with which to generate a foreign key to target column 'id'

I clearly have the user table and the id column. i have even tried by manually creating the user table in database but i still get the same error. Not sure where i am going wrong. I have also tried the solutions provided on SO

One-to-one relationship in Flask

https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#one-to-one

but still am unable to solve this error.

krishna
  • 405
  • 6
  • 25

1 Answers1

0

Flask can't find the table because you have not defined tablenames.

Example:

class Employee(db.Model):
    __tablename__ = 'employees'  # <-- whatever table name you want 
    employee_id = db.Column(db.Integer, primary_key=True)

class User(UserMixin, db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True) 

This SO post gives more details.

above_c_level
  • 3,579
  • 3
  • 22
  • 37