
I edit it to look like this: from import declarative_base Base = declarative_base () class Snickers ( Base ): _tablename_ = 'snickers' id = Column ( Integer, primary_key = True ) weight = Column ( Integer, nullable = False ) bought = Column ( DateTime ( timezone = True ), nullable = False ) eaten = Column ( DateTime ( timezone = True ), nullable = False ) The original Marathon model looks like this: from import declarative_base Base = declarative_base () class Marathon ( Base ): _tablename_ = 'marathon' id = Column ( Integer, primary_key = True ) weight = Column ( Integer, nullable = False ) bought = Column ( DateTime ( timezone = True ), nullable = False ) eaten = Column ( DateTime ( timezone = True ), nullable = False ) I want to rename the model to Snickers and have my DB table called snickers. I have an SQLAlchemy model called Marathon which represents a DB table marathon.
POSTGRESQL RENAME TABLE UPDATE
Marathon bars were renamed to Snickers in 1990 in the UK so it makes sense to update my software to reflect this, this will help avoid confusion with legacy chocolate bar naming. Let’s say I have business-critical software which tracks when I buy and eat Marathon bars. My normal workflow with Alembic is:Īlembic -c alembic.ini revision -autogenerate -m "Migration description" I’m a fan of SQLAlchemy and Postgres, I like to use them with Alembic to manage my database migrations. This includes renaming all references to the old table name such as sequences and indexes.


In this article I’ll discuss the approach I take to rename Postgres tables using Alembic. Pete Graham - CTO - Rename Postgres table with Alembic migrations Articles TIL Work About CV Rename Postgres table with Alembic migrations November 27, 2015
