将SQLAlchemy与Oracle和Flask一起使用,为主键

时间:2016-03-03 14:48:24

标签: python oracle flask sqlalchemy flask-sqlalchemy

当我将SQLAlchemy与Oracle一起使用时,我还必须为主键添加序列,但迁移不是自己创建序列。如何才能创建序列?

我已多次尝试调整代码以使SQLAlchemy为主键创建oracle序列,但到目前为止,我还无法获得SQLAlchemy创建的Oracle序列。到目前为止,我有一个非常简单的用户/角色设置,并且表存在,但不是序列。它运行时没有显示错误。

Model类如下所示:

class Role(SurrogatePK, Model):
    """A role for a user."""

    __tablename__ = 'roles'
    id = db.Column(db.Integer, db.Sequence(__tablename__ + '_id_seq'), primary_key=True)
    name = Column(db.String(80), unique=True, nullable=False)
    user_id = reference_col('users', nullable=True)
    user = relationship('User', backref='roles')

    def __init__(self, name, **kwargs):
        """Create instance."""
        db.Model.__init__(self, name=name, **kwargs)

我正在使用Flask和SQLAlchemy以及我运行之后;

$ python manage.py db init
Creating directory <lots removed here>...done

$ $ python manage.py db migrate
INFO  [alembic.runtime.migration] Context impl OracleImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'users'
INFO  [alembic.autogenerate.compare] Detected added table 'roles'

我看到没有错误,一切看起来都不错。但是,在我跑完之后;

$ python manage.py db upgrade
INFO  [alembic.runtime.migration] Context impl OracleImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 61ca5eb70d06, empty message

我第一次尝试创建记录时,它失败并显示:

sqlalchemy.exc.DatabaseError DatabaseError:(cx_Oracle.DatabaseError)ORA-02289:序列不存在

如果我手动创建序列,它可以正常工作。

1 个答案:

答案 0 :(得分:2)

感谢univerio,我发现alembic不会为你创建序列。所以,基于我的搜索,并提出了这个解决方案:

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###

    # not sure of the sequence for creating an object, so just called execute below.
    # op.execute(sa.schema.CreateSequence(sa.Sequence("users_id_seq")))

    op.execute("create sequence roles_id_seq start with 1 increment by 1 nocache nocycle")
    op.execute("create sequence users_id_seq start with 1 increment by 1 nocache nocycle")

并降级:

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.execute(sa.schema.DropSequence(sa.Sequence("roles_id_seq")))
    op.execute(sa.schema.DropSequence(sa.Sequence("users_id_seq")))

正如您所看到的,不确定使用nocache创建序列的语法是什么,因此我直接调用了SQL。这有效,并创建了所需的序列。