LoginSignup
1
0

More than 1 year has passed since last update.

python alembicを使用して、テーブル内の型を変更する

Last updated at Posted at 2021-08-16

alembicの$ alembic revision --autogenerateではデータ型の変更を認識してくれないため、revisionファイルに型を明示的に宣言する必要がある。
下の例では、column_nameカラムの型を String(255)からText()型へ変更する。

alembic_revision_file
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('table_name', 'column_name', 
        existing_type=sa.String(255),
        type_=sa.Text()
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('table_name', 'column_name', 
        existing_type=sa.Text(),
        type_=sa.String(255),
    )
    # ### end Alembic commands ###

参考文献:https://eshlox.net/2017/08/06/alembic-migration-for-string-length-change

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0