SQLAlchemy: How to add column to existing table?

家住魔仙堡 提交于 2020-05-15 10:18:46

问题


I am trying to add a field or column to an existing table using SQLAchemy.

Below is the table class

class ReleaseVersion(Base):

    __tablename__ = 'versions'

    id = Column(Integer, primary_key=True, autoincrement=True)
    release = Column(String(128), nullable=False, unique=True)


    def __init__(self,release, id=None):

        if(id):
            self.id = id

        self.release = release

I initialized the table using following line

myDB.ReleaseVersion.__table__.create(bind=self.engine, checkfirst=True)

After using the DB for some time, I need to add a boolean field, 'is_currentversion' while keeping all the existing content of the table, but I am not so sure how to do this.

Should I manually create the field to the table an update the class? Alternatively, add the field in the table class and add column if it does not exist in the initialization function?


回答1:


What you’re looking for is called a database migration. Using something like Flask-Migrate (written by Miguel Grinberg using the alembic module) allows you to change the database schema - adding or deleting columns - without losing your data. It also versions these database migrations so you can revert back if necessary.

Flask-Migrate uses the Alembic module so they both have the same functionality, Flask-Migrate is used to correctly setup alembic with your Flask and SQL-Alchemy application.

If your app is pre-production:

Here is a great video by Pretty Printed for setting up Flask-Migrate. Note that Flask-Migrate is intended for Flask applications that are using SQL-Alchemy as the ORM.

If your app is post-production:

(information from Miguel Grinberg's answer to "How do you add migrate an existing database with alembic/flask-migrate if you did not start off with it"?)

You have two options.

1) If you only want to track database migrations going forward

Run flask db init, to create the migration repository. Add the new column to your database model. Run flask db migrate, to generate a migration. The migration script will only have the new column in it. Run flask db upgrade to apply the new migration to your database. At this point your database should have the new column, and you can continue working. Repeat the above steps any time you need to make additional changes.

Note that with this approach, you cannot recreate the entire database from scratch. You have to have a way to initialize the database to the schema you had on Day 1, and then you can apply the migration history to that to upgrade it to your current schema.

2) If you want to keep track of the entire migration history, including the schema on the day you are adding Flask-Migrate to your application.

This is a little bit tricky, but it can be done.

Start with flask db init, to create the migration repository. Next, you need to trick Flask-Migrate into thinking your database is empty. You can do this by renaming your actual db, and creating a new db with the same name that has no tables in it. In that state, run flask db migrate. This will generate a migration that contains the entire schema of your database. Once you have that initial migration, restore your database to the correct state. Run flask db stamp head to mark the database as updated. Add the new column to your database model. Run flask db migrate again, to generate a second migration. The migration script will only have the new column in it. Run flask db upgrade to apply the new migration to your database.



来源:https://stackoverflow.com/questions/55271239/sqlalchemy-how-to-add-column-to-existing-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!