Reputation: 3208
I am new to sqlalchemy
but have used it now successfully to get started.. how ever I feel I am perhaps needlessly doubling up the declartion of my ORM's columns... is the table needed for an ORM object created automatically. ?
from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
TABLE_NAME = "my_hats"
def create_my_hat_table(pg_control):
pg_control.create_table(TABLE_NAME,
Column('creation_datetime', DateTime, primary_key=True, nullable=False),
Column('id', Integer, primary_key=True, nullable=False),
Column('hat_color', String))
class Hat(Base):
__tablename__ = TABLE_NAME
creation_datetime = Column(DateTime, primary_key=True, nullable=False)
id = Column(Integer, primary_key=True, nullable=False)
hat_color = Column('hat_color', String)
Elsewhere in my code is a wrapper class with the session object that I use to execute likeso
def create_table(self, table_name, *args):
new_table = Table(table_name, self.__meta, *args)
with self.__db_engine.connect() as conn:
new_table.create()
Upvotes: 1
Views: 1032
Reputation: 3208
Indeed Snakecharmerb was correct
Calling Base.metadata.create_all(engine) creates the tables declared with Base as their superclass. See also alembic applying incremental changes to a database.
Upvotes: 2