I have two tables named users and permissions and I wanted to create a relationship between them using a table named userPermissions. here's how my code looks like:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
first_name = Column(Text)
last_name = Column(Text, nullable=True)
class Permission(Base):
__tablename__ = 'permissions'
id = Column(Integer, primary_key=True)
title = Column(String(64))
allow_anonymous = Column(Boolean)
class UserPermission(Base):
__table__ = 'userPermissions'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'))
permission_id = Column(Integer, ForeignKey('permissions.id'))
value = Column(Boolean)
I know I might be doing relationships incorrectly but looking through docs and searching, I couldn't find what it is. When I try to create the tables using db.Base.metadata.create_all(db.engine) it gives me the following error:
/usr/bin/python3.6 /path/project/out.py
Traceback (most recent call last):
File "/path/project/out.py", line 1, in <module>
from components.database import setup
File "/path/project/components/database/__init__.py", line 41, in <module>
class UserPermission(Base):
File "/home/user/.local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/api.py", line 65, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/home/user/.local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 116, in _as_declarative
_MapperConfig.setup_mapping(cls, classname, dict_)
File "/home/user/.local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 144, in setup_mapping
cfg_cls(cls_, classname, dict_)
File "/home/user/.local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 172, in __init__
self._setup_table()
File "/home/user/.local/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 481, in _setup_table
if not table.c.contains_column(c):
AttributeError: 'str' object has no attribute 'c'
Where is the problem?