是否可以在 SQLAlchemy 中卸载声明类?
我正在开发一个库,用户可以简单地声明一些由数据库自动支持的类。简而言之,隐藏在代码中的某处,
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class LibraryBase(Base):
# important library stuff
用户应该这样做。
class MyStuff(LibraryBase):
# important personal stuff
class MyStuff_2(LibraryBase):
# important personal stuff
mystuff = MyStuff()
Library.register(mystuff)
mystuff.changeIt() # apply some changes to the instance
Library.save(mystuff) # and save it
# same for all other classes
在静态环境中,例如用户创建了一个包含所有个人类的文件并导入该文件,这工作得很好。所有类名称都是固定的,并且 SQLAlchemy 知道如何映射每个类。
在交互式环境中,情况有所不同:现在,一个类有可能被定义两次。两个类可能有不同的模块;但 SQLAlchemy 仍然会抱怨:
SAWarning:类名“MyStuff”已在此声明性基础的注册表中,映射到
类“OtherModule.MyStuff”>
有办法解决这个问题吗?我能否以某种方式从其 declarative_base
中卸载一个类,以便我可以将其定义与新的定义交换?
I’m working on a library where the user shall be able to simply declare a few classes which are automatically backed by the database. In short, somewhere hidden in the code, there is
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class LibraryBase(Base):
# important library stuff
and the user should then do
class MyStuff(LibraryBase):
# important personal stuff
class MyStuff_2(LibraryBase):
# important personal stuff
mystuff = MyStuff()
Library.register(mystuff)
mystuff.changeIt() # apply some changes to the instance
Library.save(mystuff) # and save it
# same for all other classes
In a static environment, e.g. the user has created one file with all personal classes and imports this file, this works pretty well. All class names are fixed and SQLAlchemy knows how to map each class.
In an interactive environment, things are different: Now, there is a chance of a class being defined twice. Both classes might have different modules; but still SQLAlchemy will complain:
SAWarning: The classname 'MyStuff' is already in the registry of this declarative base, mapped to < class 'OtherModule.MyStuff' >
Is there a way to deal with this? Can I somehow unload a class from its declarative_base
so that I can exchange its definition with a new one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用:
第一行是为了防止意外使用您未注册的课程。第二个取消注册并将阻止警告。
You can use:
The first line is to prevent accidental use of your unregisted class. The second unregisters and will prevent the warning.
看起来,我不确定这是否有效,但我认为你想要的是
sqlalchemy.orm.instrumentation.unregister_class()
http://hg.sqlalchemy.org/sqlalchemy/file/762548ff8eef/lib/sqlalchemy/orm/instrumentation .py#l466
It looks like, And I'm not really sure this even works, but I think what you want is
sqlalchemy.orm.instrumentation.unregister_class()
http://hg.sqlalchemy.org/sqlalchemy/file/762548ff8eef/lib/sqlalchemy/orm/instrumentation.py#l466
在我的项目中我使用这个解决方案。
其中库指定由
declared_attr
定义为 mixin 的列,以及由type
调用创建的目标映射器,因此我拥有完整功能的映射器。In my project I use this solution.
Where library specified columns defined as mixin by
declared_attr
and target mapper created bytype
call with bases, as result I have full functional mapper.