Postgresql Alembic autogenerate不生成升级脚本
我正在烧瓶应用程序中使用sqlalchemy和postgressql。我使用的迁移工具是alembic=0.6.3。 如果我键入Postgresql Alembic autogenerate不生成升级脚本,postgresql,database-migration,alembic,Postgresql,Database Migration,Alembic,我正在烧瓶应用程序中使用sqlalchemy和postgressql。我使用的迁移工具是alembic=0.6.3。 如果我键入alembic current,它会显示: Current revision for postgres://localhost/myDb: None 这是正确的数据库连接。但是当我运行alembic revision--autogenerate-m'adduser table'时,它会生成默认的alembic模板,其中没有任何sql命令。比如: """Add user
alembic current
,它会显示:
Current revision for postgres://localhost/myDb: None
这是正确的数据库连接。但是当我运行alembic revision--autogenerate-m'adduser table'
时,它会生成默认的alembic模板,其中没有任何sql命令。比如:
"""Add user table
Revision ID: 59b6d3503442
Revises: None
Create Date: 2015-04-06 13:42:24.540005
"""
# revision identifiers, used by Alembic.
revision = '59b6d3503442'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
我找不到任何合适的解决办法。
这是我的env.py
:
from __future__ import with_statement
from logging.config import fileConfig
import os
import sys
import warnings
from alembic import context
from sqlalchemy import create_engine, pool
from sqlalchemy.exc import SAWarning
ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
)
sys.path.append(ROOT)
from myApp import Application
from myApp.extensions import db
# Don't raise exception on `SAWarning`s. For example, if Alembic does
# not recognize some column types when autogenerating migrations,
# Alembic would otherwise crash with SAWarning.
warnings.simplefilter('ignore', SAWarning)
app = Application()
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
target_metadata = db.metadata
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = app.config['SQLALCHEMY_DATABASE_URI']
context.configure(url=url)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
url = app.config['SQLALCHEMY_DATABASE_URI']
engine = create_engine(url, poolclass=pool.NullPool)
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)
try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
这是我的alembic.ini
:
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = myApp/migrations
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
有关更多信息,请添加文件
这是extensions.py
文件,我从中为元数据导入db
:
from flask.ext.sqlalchemy import SQLAlchemy
from raven.contrib.flask import Sentry
from sqlalchemy_utils import force_instant_defaults
db = SQLAlchemy()
sentry = Sentry()
force_instant_defaults()
这是模型user.py
文件:
-*- coding: utf-8 -*-
from datetime import datetime
from flask.ext.login import UserMixin
from sqlalchemy_utils.types.password import PasswordType
from ..extensions import db
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(
db.Unicode(255),
nullable=False
)
surname = db.Column(
db.Unicode(255),
nullable=False
)
email = db.Column(
db.Unicode(255),
nullable=False,
unique=True
)
password = db.Column(
PasswordType(128, schemes=['sha512_crypt']),
nullable=False
)
created_at = db.Column(
db.DateTime,
nullable=False,
default=datetime.utcnow
)
def is_active(self):
return True
def __repr__(self):
return '<{cls} email={email!r}>'.format(
cls=self.__class__.__name__,
email=self.email,
)
def __str__(self):
return unicode(self).encode('utf8')
def __unicode__(self):
return self.email
#
-*-编码:utf-8-*-
从日期时间导入日期时间
从flask.ext.login导入UserMixin
从sqlalchemy_utils.types.password导入密码类型
从..扩展导入数据库
类用户(db.Model,UserMixin):
id=db.Column(db.Integer,主键=True)
name=db.Column(
db.Unicode(255),
nullable=False
)
姓氏=db.Column(
db.Unicode(255),
nullable=False
)
email=db.Column(
db.Unicode(255),
nullable=False,
唯一=真
)
password=db.Column(
PasswordType(128,schemes=['sha512_crypt']),
nullable=False
)
在=db.列处创建(
db.DateTime,
nullable=False,
默认值=datetime.utcnow
)
def处于活动状态(自):
返回真值
定义报告(自我):
返回“”格式(
cls=self.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
email=self.email,
)
定义(自我):
返回unicode(self).encode('utf8')
def ___; unicode(自):
返回self.email
在env.py
中导入您的sqlalchemy模型。对于这种情况,在env.py