0

I'm working for the first time with alembic and I want to insert data into a table after doing a migration.

This is my code

def upgrade():
acl_table = op.create_table('mqtt_acl',
                sa.Column('created_at', sa.DateTime(), nullable=True),
                sa.Column('updated_at', sa.DateTime(), nullable=True),
                sa.Column('id', sa.Integer(), nullable=False),
                sa.Column('allow', sa.Integer(), nullable=False),
                sa.Column('ipaddr', sa.String(60), nullable=True),
                sa.Column('username', sa.String(100), nullable=True),
                sa.Column('clientid', sa.String(100), nullable=True),
                sa.Column('access', sa.Integer(), nullable=True),
                sa.Column('topic', sa.String(100), nullable=True),
                sa.PrimaryKeyConstraint('id', name=op.f('pk_mqtt_acl'))
                )
### end Alembic commands ###

op.bulk_insert(acl_table,
              [
               {
                'id': 2,
                'username': "USERNAME",
                'clientid': "CLIENTID",
                'allow': 1,
                'access': 1,
                'topic': '#',
                }
               ]
              )

Then I run alembic upgrade head and it work's fine, doesn't return any error, but when I query the DB the data wasn't inserted and I can't figure out what's wrong.

I know the code is detected because if a make an invalid bulk_insert it gives me an invalid syntax error.

Can any one help me?

  • What exactly do you see when you run `alembic upgrade head`? Have you reverted to previous state before retrying? In other words what does `current` show you, compared to your WIP revision. – Ilja Everilä Aug 22 '17 at 11:56
  • In addition you'd get the syntax error even though you'd moved past the revision in progress, because Alembic has to parse the revision Python modules anyhow, or so I've understood it. – Ilja Everilä Aug 22 '17 at 12:13
  • @IljaEverilä I get this message `INFO [alembic.runtime.migration] Context impl PostgresqlImpl. INFO [alembic.runtime.migration] Will assume transactional DDL.` – Gabriel Oliveira Aug 22 '17 at 13:05
  • If that's it, then you've probably already run your migration once, and it has erred or you've changed it after. Compare what `alembic current` shows to your revision in progress. – Ilja Everilä Aug 22 '17 at 13:08
  • The `alembic current` returns the same thing. And I don't understand what you mean with " shows to your revision in progress" – Gabriel Oliveira Aug 22 '17 at 13:14
  • Compare the current revision number against the revision number of the migration file you're working on. If the current revision **is** the same that you're working on, you've already run that migration and changing it has no effect. You have to downgrade to previous revision and re-run if you want your changes to apply. Or create a new revision that has your changes and upgrade to that. – Ilja Everilä Aug 22 '17 at 13:15
  • Ok, got it now :) The numbers are different – Gabriel Oliveira Aug 22 '17 at 13:19
  • You still might've run it, and just moved ahead. The output you gave indicates that your DB is up to date already, i.e. all migrations have been applied. – Ilja Everilä Aug 22 '17 at 13:23

0 Answers0