Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Generating Migrations using alembic

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!

like image 545
user1342336 Avatar asked Jun 24 '12 18:06

user1342336


People also ask

What is database migration alembic?

Alembic is a lightweight database migration tool for SQLAlchemy. It is created by the author of SQLAlchemy and it has become the de-facto standard tool to perform migrations on SQLAlchemy backed databases.

What is alembic Python used for?

Alembic is a very useful library widely used for database migration. It can be used to create tables, insert data or even migrate functions from one schema to another. To be able to do all these tasks, the library uses SQLAlchemy, an ORM that is suited for working with PostgreSQL and other relational databases.

How do I reset my alembic migrations?

Go to versions/ inside migrations/ . Type flask db migrate again. Should work.

How do I change my alembic head?

Delete (or move to another folder) the specific migration file (in migrations/versions folder). The head will automatically revert to the most recent remaining migration. Using stamp will set the db version value to the specified revision; not alter the head revision number.


1 Answers

"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.

like image 80
cdaddr Avatar answered Sep 22 '22 16:09

cdaddr