Question

In the tutorial: http://alembic.readthedocs.org/en/latest/tutorial.html I tested Auto Generating Migrations function by below command:

alembic revision --autogenerate -m "Added account table"

and got error:

Traceback (most recent call last):
File "/usr/local/bin/alembic", line 9, in <module>
load_entry_point('alembic==0.3.4', 'console_scripts', 'alembic')()
File "/usr/local/lib/python2.7/dist-packages/alembic-0.3.4-py2.7.egg/alembic/config.py", line 229, in main
 **dict((k, getattr(options, k)) for k in kwarg)
File "/usr/local/lib/python2.7/dist-packages/alembic-0.3.4-py2.7.egg/alembic/command.py", line 93, in  revision
script.run_env()
File "/usr/local/lib/python2.7/dist-packages/alembic-0.3.4-py2.7.egg/alembic/script.py", line 188, in run_env
 util.load_python_file(self.dir, 'env.py')
File "/usr/local/lib/python2.7/dist-packages/alembic-0.3.4-py2.7.egg/alembic/util.py", line 185, in load_python_file
module = imp.load_source(module_id, path, open(path, 'rb'))
File "alembic/env.py", line 20, in <module>
from myapp.mymodel import Base
ImportError: No module named myapp.mymodel

I am just learning alembic, and also have never used python. Is the myapp.mymodel already there, or I need to create that using python. How to do that? Thank you very much!

Was it helpful?

Solution

"Is the myapp.mymodel already there, or I need to create that using python. How to do that?" -- if you're asking that, it sounds as if you do not yet have anything that you need to migrate.

The idea of a migration, à la Alembic, goes like this:

  1. First you have your data model defined in your python code, usually by a bunch of class declarations making use of sqlalchemy's 'declarative' modeling constructs. This occurs in a file called 'mymodel.py'. Or if your app is larger, you might have multiple files for this, and then import them all into mymodel.py to bring their symbols into one convenient namespace. This mymodel.py -- or whatever you name it -- would then be inside of a directory called myapp. You would indicate to python that the 'myapp' is a module by putting an __init__.py file in it (can be empty, or can have other stuff in it... read up on python project and module structure for more on this, see link in #3 below).

  2. At some later time you have changed your model definition in that file or files, which has left your actual database schema (as your database engine sees it, like the way you would see it in a GUI or command-line database management client) a step behind. So now you need a system to issue the necessary commands to fix up the differences.

  3. That is where Alembic comes in. It needs to look at two things: your actual database, which is why you give it your database connection string, and your data model definition, which it expects to find in a python file such as mymodel.py or whatever name you may give it. It can be named anything ending in .py, and it can be located any way you wish, as long as you can get Alembic to import it. If the mechanics of doing that are unclear to you, it's just a general python idiom that you need to learn -- how modules are structured in the filesystem and how to import them accordingly. Here is a start on that: http://docs.python.org/tutorial/modules.html

If you don't have anything that you already recognize as the python file containing your model declarations, it might be the case that you just need more practice with sqlalchemy before you worry about issues like migration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top