I'm just starting to learn SQL Alchemy and for my first project my goal is to create two separate SQLITE DB files, each with one table. On my first attempt I tried the following:
Base = declarative_base()
class HostInfo(Base):
__tablename__ = 'hosts'
id = Column(Integer, primary_key = True)
...
host_info_engine = create_engine('sqlite:///hostinfo.db', echo=False)
Base.metadata.create_all(host_info_engine)
class RecoveryLogger(Base):
__tablename__ = 'recovery_entries'
id = Column(Integer, primary_key=True)
....
recovery_log_engine = create_engine('sqlite:///recovery_logger.db', echo=False)
Base.metadata.create_all(recovery_log_engine)
For the most part this worked, but one of the unintended side-effects was that it created two tables in hostinfo.db, and that's not what I wanted - I only wanted one table in each of the database files. After a bunch of head scratching, I was able to inspect Base and I found that it contained all of the columns from both of my class declarations. The way I went about fixing this was to insert another Base = declarative_base() statement before the second class declaration (RecoveryLogger) and that seemed to fix the issue.
It threw me for a loop because I didn't think that information from the child class would make its way back up to the parent class, but it seems like it does, or at least Base seem to be some kind of special object that grabs all the details from any class that is based off of it.
So, what is the right way to go about this? Was inserting a second Base = declarative_base() statement the correct way to go about it? Should I use a different name (instead of Base) for each declarative_base() assignment?
Thanks!