0

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"],
  ...
}
2
  • Does this answer your question? dynamically creating a set of SQLAlchemy tables Commented Nov 1, 2021 at 8:24
  • already stumbled upon this question, but it states: "Is there a simple way to create this set of tables, all with the same column definitions (but each with its own name), without repeating the table definition?". This is not the case for my problem. Updated the title accordingly. Commented Nov 1, 2021 at 8:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.