Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:50:29+00:00 2026-06-18T19:50:29+00:00

How can I add an element to an Enum field in an alembic migration

  • 0

How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I’m not quite sure how best to translate that using alembic.

This is what I have:

new_type = sa.Enum('nonexistent_executable', 'output_limit_exceeded',
                   'signal', 'success', 'timed_out', name='status')
old_type = sa.Enum('nonexistent_executable', 'signal', 'success', 'timed_out',
                   name='status')
tcr = sa.sql.table('testcaseresult',
                   sa.Column('status', new_type, nullable=False))


def upgrade():
    op.alter_column('testcaseresult', u'status', type_=new_type,
                    existing_type=old_type)


def downgrade():
    op.execute(tcr.update().where(tcr.c.status==u'output_limit_exceeded')
               .values(status='timed_out'))
    op.alter_column('testcaseresult', u'status', type_=old_type,
                    existing_type=new_type)

The above unfortunately only produces ALTER TABLE testcaseresult ALTER COLUMN status TYPE status upon upgrade, which essentially does nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:50:30+00:00Added an answer on June 18, 2026 at 7:50 pm

    I decided to try to follow the postgres approach as directly as possible and came up with the following migration.

    from alembic import op
    import sqlalchemy as sa
    
    old_options = ('nonexistent_executable', 'signal', 'success', 'timed_out')
    new_options = sorted(old_options + ('output_limit_exceeded',))
    
    old_type = sa.Enum(*old_options, name='status')
    new_type = sa.Enum(*new_options, name='status')
    tmp_type = sa.Enum(*new_options, name='_status')
    
    tcr = sa.sql.table('testcaseresult',
                       sa.Column('status', new_type, nullable=False))
    
    
    def upgrade():
        # Create a tempoary "_status" type, convert and drop the "old" type
        tmp_type.create(op.get_bind(), checkfirst=False)
        op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE _status'
                   ' USING status::text::_status')
        old_type.drop(op.get_bind(), checkfirst=False)
        # Create and convert to the "new" status type
        new_type.create(op.get_bind(), checkfirst=False)
        op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE status'
                   ' USING status::text::status')
        tmp_type.drop(op.get_bind(), checkfirst=False)
    
    
    def downgrade():
        # Convert 'output_limit_exceeded' status into 'timed_out'
        op.execute(tcr.update().where(tcr.c.status==u'output_limit_exceeded')
                   .values(status='timed_out'))
        # Create a tempoary "_status" type, convert and drop the "new" type
        tmp_type.create(op.get_bind(), checkfirst=False)
        op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE _status'
                   ' USING status::text::_status')
        new_type.drop(op.get_bind(), checkfirst=False)
        # Create and convert to the "old" status type
        old_type.create(op.get_bind(), checkfirst=False)
        op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE status'
                   ' USING status::text::status')
        tmp_type.drop(op.get_bind(), checkfirst=False)
    

    It appears that alembic has no direct support for the USING statement in its alter_table method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can add an element using javascript by: document.createElement('input'); But I want
Can I add an object of Elements inside another DOM element using grab or
I have a <select> element which has some initial values, how can I add
I have a few dynamic queue in my program which can add element at
I can't access a element with its href. Like this example, i need add
.append Function adds elements to the list. How can I add elements to the
We can add an image to an photo album using UIImage *img = [UIImage
I wrote library which can add and update objects in salesforce. I use beatbox
How can I add an element to the beginning of array without changing array
I know that for arrays you can add an element in a two dimensional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.