I installed alembic 0.3.4, sqlalchemy, SQLite version 3.7.4, and upgraded SQLAlchemy 0.6.4 to SQLAlchemy 0.7 or greater from my ubuntu. I followed the instructions linked here:
Now I am testing: Auto Generating Migrations I have created a package: schemas, and a package marker under schemas: init.py with one line defined:
__all__ = ["teacher"]
I also created a module file: dbmodel.py in schemas directory with below content
Base = declarative_base()
class teacher(Base):
__tablename__ = 'teacher'
id = Column(Integer, primary_key=True)
name = Column(String)
department = Column(String)
By the way, I have a sqlite db created, and it is running fine for doing some test before Auto Generating Migrations. I configured the env.py file. There are two lines added:
from schemas.dbmodel import Base
target_metadata = Base.metadata
Then I run:
alembic revision --autogenerate -m "Added teacher table"
but still get 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 schemas.dbmodel import Base
ImportError: No module named schemas.dbmodel
I don't know why it is so difficult for me to test a simple example using alembic. I just want to import my application data model into the physical database model. Is that so complicated? Thanks. Please somebody who knows alembic gives us a simple exaple step by step. I guess more people will get benefit from that.
Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python. Front Matter. Project Homepage. Installation. Dependencies.
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.
Show activity on this post. 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.
I also found that Alembic couldn't find my model modules. As a workaround, I found that, by adding the following to my env.py
before importing my models, I could force it to work:
import os, sys
sys.path.append(os.getcwd())
This is probably not the best solution, but it got Alembic to autogenerate my migrations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With