alembic and getting the last inserted value

前端 未结 1 1403
不知归路
不知归路 2021-02-10 18:00

I\'m using alembic to manage my database structure.

After adding a table using id as Integer and primary key the id column will be an autoincrement-column. How do I quer

1条回答
  •  孤街浪徒
    2021-02-10 18:24

    First to have auto-increment column you'll need to modify table schema definition to have Sequence passed for primary key column: sa.Column('id', sa.Integer, Sequence('srv_feed_r_t_seq'),primary_key=True),

    #creating the table
    op.create_table(
        'srv_feed_return_type',
        sa.Column('id', sa.Integer, Sequence('srv_feed_r_t_seq'),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)
    )
    

    Now about how to get PK id:

    op.execute and op.bulk_insert doesn't return you any results. But you can get same connection used for these operations.

    After doing bulk_insert or execute(table.update ...) you can run select query in the same context and retrieve PK id for the record of interest:

    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"""
    

    You'll need to specify proper filter in where clause to make sure that you identified record you recently changed in unique way.

    Here is some example of similar functionality, but it has hardcoded select queries

    0 讨论(0)
提交回复
热议问题