Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python alembic并获取最后插入的值_Python_Sqlalchemy_Alembic - Fatal编程技术网

Python alembic并获取最后插入的值

Python alembic并获取最后插入的值,python,sqlalchemy,alembic,Python,Sqlalchemy,Alembic,我正在使用alembic来管理我的数据库结构 添加使用id作为整数和主键的表后,id列将是一个自动增量列。如何查询升级脚本中的数据,以确保获得正确的id(我知道在这种特定情况下id为1) 我知道怎么做 #creating the table op.create_table( 'srv_feed_return_type', sa.Column('id', sa.Integer, primary_key=True), sa.Column('name', sa.String(5

我正在使用alembic来管理我的数据库结构

添加使用id作为整数和主键的表后,id列将是一个自动增量列。如何查询升级脚本中的数据,以确保获得正确的id(我知道在这种特定情况下id为1)

我知道怎么做

#creating the table
op.create_table(
    'srv_feed_return_type',
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('name', sa.String(50), nullable=False),
    sa.Column('created', sa.DateTime, server_default=func.now(), nullable=False),
    sa.Column('created_by', sa.String(50), nullable=False),
    sa.Column('last_updated', sa.DateTime, nullable=False),
    sa.Column('last_updated_by', sa.String(50), nullable=False)
)

#table for operations
srv_feed_return_type = table('srv_feed_return_type',
                             column('name'),
                             column('created'),
                             column('created_by'),
                             column('last_updated'),
                             column('last_updated_by'))

#bulk insert
op.bulk_insert(srv_feed_return_type,
               [
                   {'name': 'dataset',
                    'created': datetime.now(), 'created_by': 'Asken',
                    'last_updated': datetime.now(), 'last_updated_by': 'Asken'}
               ])
我知道我可以做更新,但我如何做选择使用类似下面的东西

op.execute(
    srv_feed_return_type.update().\
        where(srv_feed_return_type.c.name==op.inline_literal('dataset')).\
        values({'name':op.inline_literal('somethingelse')})
        )

首先要使用自动递增列,您需要修改表架构定义,以便为主键列传递序列:
sa.column('id',sa.Integer,Sequence('srv\u feed\u r\u t\u seq'),primary\u key=True),

现在,关于如何获取PK id:

op.execute
op.bulk\u insert
不会返回任何结果。但您可以获得用于这些操作的相同连接

执行
bulk\u insert
execute(table.update…)
后,您可以在相同的上下文中运行select查询并检索感兴趣记录的PK id:

connection = op.get_bind()
r = connection.execute(srv_feed_return_type.select().where(...))
for row in r:
    pk_id = r['id']
    """or something more sophisticated"""
您需要在where子句中指定适当的筛选器,以确保以唯一的方式标识最近更改的记录


这里是,但它已经硬编码了select查询

是否可以安全地假设在迁移完成之前不会有任何其他进程修改新创建的表?对于本例,是的,但我知道这里会有一个。在另一次迁移中,它将不再是1。我需要知道特定返回类型的id。您的应用程序使用的是什么数据库,是否有可能将更多数据发送到另一个数据库?是否要求应用程序与数据库无关?将尝试。谢谢顺便说一句,通过定义Integer和primary_key=True,它将自动成为一个自动递增。感谢您提供有关自动递增的提示。当我使用Oracle时,它对我不起作用-所以我必须明确地使用序列,因为它对Oracle不同。。。也许我应该把它添加到我的代码中,以确保安全。
connection = op.get_bind()
r = connection.execute(srv_feed_return_type.select().where(...))
for row in r:
    pk_id = r['id']
    """or something more sophisticated"""