This is a follow-up to a post I opened regarding the creation of dynamic tables and columns using SQLALCHEMY. SQLAlchemy create dynamic tables and columns
Most of the tutorials that show how to create dynamic Tables and columns in Python and SQLAlchemy focus on creating a class object - using Declarative
, such that every use of a class creates a table with the pre-defined columns and column metadata defined in that class (see below). The problem with this way of creating tables is that at least as I understand it, does not allow me to create columns dynamically as well. I am reading an API and attaining metadata for tables which can change daily, therefore I need a method of creating these tables dynamically - see my orignal post: SQLAlchemy create dynamic tables and columns
The following construct for instance does not allow for creating dynamic columns for tables, as far as I understand it, such like my original post:
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
The reason I ask about class objects in this regard, is because the tutorials that show how to query the database require the use of a class object in the query.
So for instance, every method I've researched of querying the database to find the lastest value in each table (maximum id
assuming it is the primary key or maximum timestamp
) seems to require the use of a class object which currently I am not doing.
Session = sessionmaker(bind=engine)
session = Session()
User.query.order_by('timestamp desc').limit(1)
Is it possible to Is it possible to construct a class object that will create dynamic columns? Using a class object ?
If not, Is there another best-practice for querying the database using SQLAlchemy to find the latest record in a table ?
See this blog post on SQLAlchemy - Great tutorial, i've read.