Python 将SQLAlchemy与Oracle和Flask结合使用,为主键创建序列
当我在Oracle中使用SQLAlchemy时,我还必须为主键添加序列,但迁移并不是自己创建序列。如何创建序列 我曾多次尝试调整代码以获得SQLAlchemy来为主键创建oracle序列,但到目前为止,我还无法获得由SQLAlchemy创建的oracle序列。到目前为止,我有一个非常简单的用户/角色设置,表存在,但序列不存在。它在运行时不显示任何错误 模型类如下所示:Python 将SQLAlchemy与Oracle和Flask结合使用,为主键创建序列,python,oracle,flask,sqlalchemy,flask-sqlalchemy,Python,Oracle,Flask,Sqlalchemy,Flask Sqlalchemy,当我在Oracle中使用SQLAlchemy时,我还必须为主键添加序列,但迁移并不是自己创建序列。如何创建序列 我曾多次尝试调整代码以获得SQLAlchemy来为主键创建oracle序列,但到目前为止,我还无法获得由SQLAlchemy创建的oracle序列。到目前为止,我有一个非常简单的用户/角色设置,表存在,但序列不存在。它在运行时不显示任何错误 模型类如下所示: class Role(SurrogatePK, Model): """A role for a user."""
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)
我用烧瓶,炼金术,然后我跑
$ 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:序列不存在
如果我手动创建序列,效果很好。多亏了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。这很有效,并创建了所需的序列。多亏了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。这起到了作用,并创建了所需的序列。您使用的是自动生成的alembic迁移脚本吗?alembic不是一个完全自动化的解决方案。您必须手动调整创建的迁移文件,以解决它自己无法解决的问题。在这种情况下,您似乎必须手动添加代码来创建/删除序列。啊,感谢您提供的信息。您是否使用自动生成的alembic迁移脚本?alembic不是一个完全自动化的解决方案。您必须手动调整创建的迁移文件,以解决它自己无法解决的问题。在这种情况下,您似乎必须手动添加代码来创建/删除序列。啊,谢谢您提供的信息。