I’m working on a python interface to a SQL database containing electronic parts data. There is a base class representing a master part number reference table which all the part type classes inherit from. Each subclass has its own table to hold the parametric data. There are also several other classes containing info such as vendor data, of which each instance of Part has one or more instances of.
The inheritance hierarchy looks like this:
Part
PartTypeA
PartTypeB
The Part class has several Relationships with other tables describing vendor info etc.
I have a separate DatabaseManager class to handle adding parts to the database (manages and controls an instance of a SQLAlchemy engine). How do I use SQLAlchemy to issue CREATE statements for all the tables even if the tables are defined in other classes?
I have tried the following (this is in the database manager class):
self.db_engine = create_engine(db_uri, echo=False)
Part.metadata.create_all(self.db_engine)
This actually works, but I am not sure why (perhaps because Part contains SQLAlchemy Relationships to some tables and the other tables are all subclasses of Part?). The examples in the SQLAlchemy docs seem to be assuming that the engine is being defined in the same place as the database tables.
Does anyone know if this is the correct method? If not, what is the proper way to have SQLAlchemy issue the CREATE statements?
I tried porting this code to MySQL, but the create statements were not issued as they were when I used sqlite. Here’s what I did to get the code to work in MySQL: