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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top