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