Automatically create migrations using alembic

In the tutorial: http://alembic.readthedocs.org/en/latest/tutorial.html I tested the function of automatic migration generation using the following command:

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

and got the 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 never used python. Is myapp.mymodel already there, or do I need to create it using python. How to do it? Thank you very much!

+4
source share
1 answer

"Already have myapp.mymodel, or do I need to create this using python. How do I do this?" - if you ask for it, it sounds as if you do not yet have something that you need for migration.

The idea of ​​migration, à la Alembic, is as follows:

  • First you have a data model defined in python code, usually a bunch of class declarations using sqlalchemy's "declarative" modeling constructs. This happens in a file named 'Mymodel.py. Or, if your application is larger, you can have several files for this, and then import them all into mymodel.py to bring their characters into one convenient Namespace. This mymodel.py - or whatever you call it - then be inside a directory called myapp. You would tell python that "myapp" is a module by placing the __init__.py file in it (it may be empty or may have other things in it ... read in the python project and the module structure for more information see link in No. 3 below).

  • Later, you changed the definition of the model in this file or files that left your actual database schema (as your database engine sees this, similar to how you see it in the GUI or command line database management client) step by step. So, now you need a system to issue the necessary commands for the difference.

  • This is where Alembic comes in. He needs to look at two things: your actual database, so you give it a database connection string and the definition of the data model that it expects to find in a python file, for example mymodel.py, or any other name you can give it. Maybe it’s called everything that ends with .py, and it can be arranged as you if you can force Alembic to import it. If the mechanics of what you don’t understand is just a common python idiom, what you need to learn is how the modules are structured in the file system and how to import them accordingly. Here is the beginning of that: http://docs.python.org/tutorial/modules.html

If you don't have what you already know as a python file containing model declarations, you might just need more practice with sqlalchemy before you worry about issues like porting.

+20
source

All Articles