Reputation: 671
I have some migrations in alembic, and try to run alembic upgrade head
to up-to-date my DB for revision, but in some cases I have tables, which already exists, so I have an error:
alembic upgrade head
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 3137cf88d8c6, create tables
Traceback (most recent call last):
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
psycopg2.ProgrammingError: relation "City" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/toweya/.virtualenvs/numus3/bin/alembic", line 11, in <module>
sys.exit(main())
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 479, in main
CommandLine(prog=prog).main(argv=argv)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 473, in main
self.run_cmd(cfg, options)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/config.py", line 456, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/command.py", line 254, in upgrade
script.run_env()
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/script/base.py", line 416, in run_env
util.load_python_file(self.dir, 'env.py')
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/util/compat.py", line 64, in load_module_py
module_id, path).load_module(module_id)
File "<frozen importlib._bootstrap_external>", line 388, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 809, in load_module
File "<frozen importlib._bootstrap_external>", line 668, in load_module
File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "numus/env.py", line 68, in <module>
run_migrations_online()
File "numus/env.py", line 63, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/runtime/environment.py", line 817, in run_migrations
self.get_context().run_migrations(**kw)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/runtime/migration.py", line 323, in run_migrations
step.migration_fn(**kw)
File "/home/toweya/PycharmProjects/NumusPortal/portal/db_manage/numus/versions/3137cf88d8c6_create_tables.py", line 30, in upgrade
sa.Column('name', sa.String(64), nullable=False),
File "<string>", line 8, in create_table
File "<string>", line 3, in create_table
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/ops.py", line 1106, in create_table
return operations.invoke(op)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/base.py", line 318, in invoke
return fn(self, operation)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/operations/toimpl.py", line 101, in create_table
operations.impl.create_table(table)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/ddl/impl.py", line 194, in create_table
self._exec(schema.CreateTable(table))
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/alembic/ddl/impl.py", line 118, in _exec
return conn.execute(construct, *multiparams, **params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1002, in _execute_ddl
compiled
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
exc_info
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/home/toweya/.virtualenvs/numus3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "City" already exists
[SQL: '\nCREATE TABLE "City" (\n\tid SERIAL NOT NULL, \n\tgeoid INTEGER NOT NULL, \n\tname VARCHAR(64) NOT NULL, \n\tPRIMARY KEY (id), \n\tUNIQUE (geoid)\n)\n\n']
How can I ignore existing tables?
That means something like: if table exist - continue but not abort
Upvotes: 7
Views: 6517
Reputation: 6396
You need to go to your migration/version folder or alembic :
/path/to/prooject/alembic/versions/3137cf88d8c6.py
and edit the last migration script by removing the script for creating the table in the upgrade function and remove the reverse task in downgrade function in the same script.
As said in flask migrate :
The migration script needs to be reviewed and edited
And I quote this from alembic:
We review and modify these by hand as needed, then proceed normally.
Edit :
You can also do that automatically by using the include_object
as suggested by this answer
Upvotes: 6