I need to create several Tables in SQLalchemy ORM which all follow a similar schema:
- Each table contains the same set of shared columns (id +
shared_col1/2) - Each table consists of 1+ individual columns (dimensions)
- The table name and the respective dimensions are known at the time the table is created
- It can be assumed that all dimensions columns are string columns
Here's an example of a the tables are created:
class SomeTable1(Base):
__tablename__ = 'dim_table1'
id = Column(String(4), primary_key=True)
shared_col1 = Column(String)
dimension1 = Column(String, nullable=False)
dimension2 = Column(String, nullable=False)
shared_col2 = Column(String, nullable=False))
My Question: How could I create the tables dynamically, e.g. in a loop? Without having to explicitly state each table definition? Say, based on a dict like this:
{
"dimension_table1": ["dimension1", "dimension2"],
"dimension_table2": ["dimension3"],
...
}