Im working on a library where the user can simply declare several classes that are automatically supported 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 must execute
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, for example. the user created a single file with all personal classes and imports this file, this works very well. All class names are fixed, and SQLAlchemy knows how to map each class.
In an interactive environment, everything is different: now there is a chance that the class will be defined twice. Both classes can have different modules; but still SQLAlchemy will complain:
SAWarning: The class name MyStuff is already in the registry of this declarative database, displayed in <class 'OtherModule.MyStuff'>
Is there any way to handle this? Is there any way to unload a class from declarative_base so that I can exchange its definition with a new one?
python class mapping declarative sqlalchemy
Debilski
source share