尝试迁移数据库时出错(slalchemy,slqite3)

时间:2015-07-24 00:52:49

标签: python sqlite flask sqlalchemy alembic

我有下一个models.py文件:

from app import db, bcrypt
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship


class BlogPost(db.Model):

    __tablename__ = "posts"

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String, nullable=False)
    desc = db.Column(db.String, nullable=False)
    author_id = db.Column(db.Integer, ForeignKey('users.id'))

    def __init__(self, title, desc):
        self.title = title
        self.desc = desc

    def __repr__(self):
        return "Titulo >> " + self.title

class User(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    email = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)
    posts = relationship("BlogPost", backref="author")

    def __init__(self, name, email, password):
        self.name = name
        self.email = email
        self.password = password

    def __repr__(self):
        return "Usuario >> ", self.name

然后我跑了:

python manage.py db init

一切都很好,就是当我试图修改我的模型文件进行迁移时..我只是改变了一行:

self.password = password

为:

self.password = bcrypt.generate_password_hash(password)

当我跑的时候:

python manage.py db migrate

它有效,但当我尝试升级时:

python manage.py db upgrade

我收到了下一个错误:

"No support for ALTER of constraints in SQLite dialect")
NotImplementedError: No support for ALTER of constraints in SQLite dialect

注意:数据库只有一些测试记录..谢谢!

1 个答案:

答案 0 :(得分:2)

自从大多数Alembic教程编写完成以来,Alembic已经改变了SQLite迁移的方式。 ALTER命令现在需要使用batch mode。如果您决定稍后从SQLite切换,则此模式仍与其他数据库兼容。

对于要更改的每个表,将所有操作放在with op.batch_alter_table('table_name'):块中。

def upgrade():
    with op.batch_alter_table('table_name'):
        op.alter_column(...)