Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlalchemy enum migration update fails saying does not exist

I have following sqlalchemy model:

class Cart(db.Model):
    __tablename__ = 'carts'
    #...
    cart_status = db.Column(db.Enum('confirmed', 'canceled', name='cart_statuses'))

Which generates following migration script:

"""empty message

Revision ID: c7cbe7d1d686
Revises: 56e9612a77ee
Create Date: 2017-06-21 08:52:00.987769

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c7cbe7d1d686'
down_revision = '56e9612a77ee'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('carts', sa.Column('cart_status', sa.Enum('confirmed', 'canceled', name='cart_statuses'), nullable=True))
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('carts', 'cart_status')
    # ### end Alembic commands ###

When I try to upgrade, I get following error:

(ecom_bot) root@logicandthoughts:~/ecom/ecombot# python manage.py db upgrade
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 56e9612a77ee -> c7cbe7d1d686, empty message
Traceback (most recent call last):
  File "manage.py", line 37, in <module>
    manager.run()
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/flask_migrate/__init__.py", line 247, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/script/base.py", line 421, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/util/compat.py", line 75, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 87, in <module>
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/runtime/migration.py", line 329, in run_migrations
    step.migration_fn(**kw)
  File "/root/ecom/ecombot/migrations/versions/c7cbe7d1d686_.py", line 21, in upgrade
    op.add_column('carts', sa.Column('cart_status', sa.Enum('confirmed', 'canceled', name='cart_statuses'), nullable=True))
  File "<string>", line 8, in add_column
  File "<string>", line 3, in add_column
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/operations/ops.py", line 1551, in add_column
    return operations.invoke(op)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/operations/base.py", line 318, in invoke
    return fn(self, operation)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/operations/toimpl.py", line 123, in add_column
    schema=schema
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 172, in add_column
    self._exec(base.AddColumn(table_name, column, schema=schema))
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/alembic/ddl/impl.py", line 118, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1002, in _execute_ddl
    compiled
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
    exc_info
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/root/ecom/ecom_bot/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type "cart_statuses" does not exist
LINE 1: ALTER TABLE carts ADD COLUMN cart_status cart_statuses
                                                 ^
 [SQL: 'ALTER TABLE carts ADD COLUMN cart_status cart_statuses']
like image 494
sadaf2605 Avatar asked Jun 21 '17 08:06

sadaf2605


1 Answers

I had to update to following:

"""empty message

Revision ID: 51aa3bff68d6
Revises: c7cbe7d1d686
Create Date: 2017-06-21 09:02:55.252361

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '51aa3bff68d6'
down_revision = 'c7cbe7d1d686'
branch_labels = None
depends_on = None


def upgrade():
    cart_status = postgresql.ENUM('user_unconfirmed', 'user_confirmed', 'client_unconfirmed', 'client_confirmed', name='cart_status')
    cart_status.create(op.get_bind())


    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('carts', sa.Column('cart_status', sa.Enum('user_unconfirmed', 'user_confirmed', 'client_unconfirmed', 'client_confirmed', name='cart_status'), nullable=True))
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('carts', 'cart_status')
    # ### end Alembic commands ###
    cart_status = postgresql.ENUM('user_unconfirmed', 'user_confirmed', 'client_unconfirmed', 'client_confirmed', name='cart_status')
    cart_status.drop(op.get_bind())
like image 57
sadaf2605 Avatar answered Nov 11 '22 17:11

sadaf2605