如何摆脱sqlalchemy.exc.NoReferencedTableError?
How to get rid of sqlalchemy.exc.NoReferencedTableError?
这里有一些与问题相关的代码:
db_session.py
import sqlalchemy as sa
import sqlalchemy.orm as orm
import sqlalchemy.ext.declarative as dec
SqlAlchemyBase = dec.declarative_base()
__Session = None
def create_session():
global __Session
global_init()
return __Session()
def global_init():
global __Session
if __Session:
return
conn_str = f'sqlite:///sqlite.db'
engine = sa.create_engine(conn_str, echo=True, future=True)
__Session = orm.sessionmaker(bind=engine)
SqlAlchemyBase.metadata.create_all(engine)
User.py(错误来自 User.department_id 列)
import sqlalchemy
from db_session import SqlAlchemyBase
class User(SqlAlchemyBase):
__tablename__ = 'users'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
department_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey('departments.id'), nullable=True)
Departments.py
import sqlalchemy
from db_session import SqlAlchemyBase
class Departments(SqlAlchemyBase):
__tablename__ = 'departments'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
chief = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey("users.id"))
members = sqlalchemy.orm.relationship("User", foreign_keys="User.department_id")
每当我尝试访问 'users' table 时就会出现错误。这是错误文本:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'users.department_id' could not find table 'departments' with which to generate a foreign key to target column 'id'
我用数据库管理软件查看了sqlite.db文件,确实有'departments'table。我打印了
的输出
SqlAlchemyBase.metadata.tables.keys()
'departments' table 也在那里,甚至还有正确的名字。我尝试向 'departments' table 添加一些内容,但这也无济于事。所有 table 的名称都明确设置为 tablename 属性,因此 sqlalchemy 的自动 table 命名不是问题。我该如何摆脱这个错误?
感谢@snakecharmerb 提供解决方案!
事实证明,我必须在任何地方导入部门 class 我使用 sqlalchemy 的用户模型来查找所有表。
这里有一些与问题相关的代码:
db_session.py
import sqlalchemy as sa
import sqlalchemy.orm as orm
import sqlalchemy.ext.declarative as dec
SqlAlchemyBase = dec.declarative_base()
__Session = None
def create_session():
global __Session
global_init()
return __Session()
def global_init():
global __Session
if __Session:
return
conn_str = f'sqlite:///sqlite.db'
engine = sa.create_engine(conn_str, echo=True, future=True)
__Session = orm.sessionmaker(bind=engine)
SqlAlchemyBase.metadata.create_all(engine)
User.py(错误来自 User.department_id 列)
import sqlalchemy
from db_session import SqlAlchemyBase
class User(SqlAlchemyBase):
__tablename__ = 'users'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
department_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey('departments.id'), nullable=True)
Departments.py
import sqlalchemy
from db_session import SqlAlchemyBase
class Departments(SqlAlchemyBase):
__tablename__ = 'departments'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
chief = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey("users.id"))
members = sqlalchemy.orm.relationship("User", foreign_keys="User.department_id")
每当我尝试访问 'users' table 时就会出现错误。这是错误文本:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'users.department_id' could not find table 'departments' with which to generate a foreign key to target column 'id'
我用数据库管理软件查看了sqlite.db文件,确实有'departments'table。我打印了
的输出SqlAlchemyBase.metadata.tables.keys()
'departments' table 也在那里,甚至还有正确的名字。我尝试向 'departments' table 添加一些内容,但这也无济于事。所有 table 的名称都明确设置为 tablename 属性,因此 sqlalchemy 的自动 table 命名不是问题。我该如何摆脱这个错误?
感谢@snakecharmerb 提供解决方案!
事实证明,我必须在任何地方导入部门 class 我使用 sqlalchemy 的用户模型来查找所有表。