我正在尝试运行alembic迁移,当我运行时
alembic revision --autogenerate -m "Added initial tables"
Run Code Online (Sandbox Code Playgroud)
它说不出来
sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver
Run Code Online (Sandbox Code Playgroud)
数据库网址是
postgresql+psycopg2://dev:passwd@localhost/db
Run Code Online (Sandbox Code Playgroud)
我甚至psycopg2安装在我的virtualenv中
$yolk -l
Flask-Login - 0.1.3 - active
Flask-SQLAlchemy - 0.16 - active
Flask - 0.9 - active
Jinja2 - 2.6 - active
Mako - 0.7.3 - active
MarkupSafe - 0.15 - active
Python - 2.7.2 - active development (/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload)
SQLAlchemy - 0.8.0 - active
Werkzeug - 0.8.3 - active
alembic - 0.4.2 - active
antiorm - 1.1.1 - active …Run Code Online (Sandbox Code Playgroud) 我可以alembic --autogenerate在添加/删除列时使用
.
但是,当我想修改例如200个字符到2000个字符的"url"列时,它不会检测到更改.
如何创建Alembic(使用SQLAlchemy),检测更改并自动生成脚本到我的模型的各种列的"大小",并为PostgreSQL创建"alter_column"命令?
编辑:
为什么不使用alembic自动添加:
op.alter_column('mytable', 'url', type_=sa.String(2000), existing_type=sa.String(length=200), nullable=True)
Run Code Online (Sandbox Code Playgroud) 我已经问了一个关于如何通过使用检测表的问题(Alembic - sqlalchemy初始迁移)
target_metadata = Base.metadata
Run Code Online (Sandbox Code Playgroud)
对于
alembic revision --autogenerate -m "initial migration"
Run Code Online (Sandbox Code Playgroud)
在我将模型导入env.py文件之后,它似乎工作正常但它没有检测到实际存在的表,因此它创建了包含所有表的迁移文件,例如:
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('Brand',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('slug', sa.String(), nullable=True),
sa.Column('date_created', sa.DateTime(), nullable=True),
sa.Column('date_updated', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
schema='Products'
)
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_table('ProductFile', schema='Products')
Run Code Online (Sandbox Code Playgroud)
我试过了:
alembic stamp head
Run Code Online (Sandbox Code Playgroud)
但在运行并运行autogenerate命令后,系统再次生成所有模型.
from project.apps.users.models import *
from project.apps.orders.models import *
from project.apps.products.models import *
from project.base …Run Code Online (Sandbox Code Playgroud) 我正在使用Alembic来处理Flask的迁移.alembic revision --autogenerate理论上,应根据数据库中的更改自动生成迁移.但是,Alembic只是使用上述命令生成空白迁移.
有一个问题与这个问题非常相似,问题在于没有导入正确的模型.但是,我从Flask应用程序导入了模型,如下所示env.py:
...
# import settings from Flask
alembic_config = config.get_section(config.config_ini_section)
from start import app
from models import User, Item, Recipient # models are imported here from models.py
alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
engine = engine_from_config(
alembic_config, # config.get_section(config.config_ini_section)
prefix='sqlalchemy.',
poolclass=pool.NullPool)
...
Run Code Online (Sandbox Code Playgroud)
以及导入的db元数据env.py('start'是我的Flask应用程序主文件的名称):
...
from start import db
target_metadata = db.metadata
...
Run Code Online (Sandbox Code Playgroud)
alembic revision --autogenerate -m "initial_rev"然后运行生成一个空迁移,虽然我的Flask应用程序会有所不同:
"""initial_rev
Revision ID: 45296fd29540
Revises: None
Create Date: 2013-06-19 17:32:38.392268
"""
# revision identifiers, …Run Code Online (Sandbox Code Playgroud)