14

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.

Peter Vandivier
  • 606
  • 1
  • 9
  • 31
user1342336
  • 967
  • 2
  • 16
  • 28
  • So where in your code is the url to the sqlite database? I got this to work from an existing pyramid application that was using sqlalchemy-migrations. In order to get this to work I had to bootstrap the url/db connection as pyramid does and then to load up my model Base from there. https://github.com/mitechie/Bookie/blob/develop/dbversions/env.py#L12 – Rick Jun 30 '12 at 18:33
  • In file: alembic.ini, I configured it and added one line: sqlalchemy.url =sqlite:///test This worked for migrating schema change to my test DB. – user1342336 Jun 30 '12 at 18:41

1 Answers1

30

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.

andyhd
  • 416
  • 4
  • 4
  • Thanks. You are right, although my code still has some problem, at least the data module path works fine now. You save me lots of time :) – user1342336 Jul 01 '12 at 14:50
  • 1
    I recently found out that we need to configure the PYTHONMATH variable by adding the current module path. It works. – user1342336 Nov 28 '12 at 19:22
  • I was facing a similar problem and now it works.. Here is the question and the answer that solved my problem http://stackoverflow.com/questions/15038036/integrating-alembic-with-sqlalchemy – Sandeep Raju Prabhakar Mar 20 '13 at 13:59
  • Tried that, I get ImportError on configuration package. here is what I have added `import os import sys sys.path.append(os.getcwd()) from configuration import app, db` – daydreamer Mar 26 '13 at 22:47