gpt4 book ai didi

python - SQLAlchemy 未映射类错误 : Class 'sqlalchemy.ext.declarative.DeclarativeMeta' is not mapped

转载 作者:行者123 更新时间:2023-11-28 20:26:28 24 4
gpt4 key购买 nike

我在我的项目中使用模型继承。

我的模型:

class CommentBase(Base):
__abstract__ = True

id = Column(Integer, primary_key=True)
text = Column(Unicode(500))

@declared_attr
def user_id(cls):
return Column(ForeignKey('user.id'))

@declared_attr
def user(cls):
return relationship('User')

def __init__(self, user_id, text):
self.user_id = user_id
self.text = text

class CommentOne(CommentBase):
__tablename__ = 'comment_one'

class CommentTwo(CommentBase):
__tablename__ = 'comment_two'

所有表都正确创建,但是当我尝试用一​​些测试数据填充表(即使是那些不从其他表继承的表)时,它会引发错误:

UnmappedClassError: Class 'sqlalchemy.ext.declarative.DeclarativeMeta' is not mapped

这是我的基础:

Base = declarative_base()

如果没有继承,一切都将按应有的方式工作。如何解决?

提前致谢!

最佳答案

我刚刚偶然发现了同样的问题。问题是我填充表格的方式:

s = application.models.Sample
s.label = 'asdf'
db_session.add(s)

UnmappedInstanceError: Class 'sqlalchemy.ext.declarative.api.DeclarativeMeta' is not mapped; was a class (application.models.Sample) supplied where an instance was required?

当我修复我的实例化时,它工作正常:

s = application.models.Sample()
...

关于python - SQLAlchemy 未映射类错误 : Class 'sqlalchemy.ext.declarative.DeclarativeMeta' is not mapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11823521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com