![]() I can rename the index in the migration too: op. ![]() I’ve got this pesky index marathon_pkey hanging about. Indexes : "marathon_pkey" PRIMARY KEY, btree ( id ) Let’s inspect the snickers table again to see our last problem: snacks =# \ d snickers. execute ( 'ALTER SEQUENCE marathon_id_seq RENAME TO snickers_id_seq' )Īre we finished? Of course not, this is a development blog post, we need at least three things to go wrong. I rename the sequence in the migration like this: op. On further inspection of the snickers table \d snickers I can see that marathon_id_seq is used for auto-incrementing the id: snacks =# \ d snickers Table "public.snickers" Column | Type | Modifiers -+-+- id | integer | not null default nextval ( 'snickers_id_seq' :: regclass ) What is the troublesome marathon_id_seq doing hanging about? Running \d shows: snacks =# \ d List of relations Schema | Name | Type | Owner -+-+-+- public | alembic_version | table | snacks public | marathon_id_seq | sequence | snacks public | snickers | table | snacks ( 3 rows ) ![]() rename_table ( 'marathon', 'snickers' )Īll seems good in the world until I take a look in Postgres. I need to manually edit the migration to use the alembic rename_table command: def upgrade (): op. ![]() This is going to be a problem if I want to keep existing data, as it’ll be deleted along with the marathon table. The migration is dropping the marathon table and creating a new snickers table. DateTime ( timezone = True ), nullable = False ), sa. This produces the following migration for upgrade: def upgrade (): op. I then run: alembic -c alembic.ini revision -autogenerate -m "Renaming Marathon model to Snickers" 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. 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
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |