flask-migrate: running custom SQL in migrations?

848 views Asked by At

I need to run some custom SQL on my table before applying a migration. In particular, I need to populate a column before declaring it non-nullable:

Clickid.query\
        .filter(Clickid.first_seen_at == None)\
        .update({'first_seen_at': datetime.utcnow()})

I've read about op.execute method in Alembic docs, where we feed raw SQL to the execute method:

op.execute(
    account.update().\
        where(account.c.name==op.inline_literal('account 1')).\
        values({'name':op.inline_literal('account 2')})
        )

however, with Flask-SQLALchemy my query does not return a string with SQL, it runs the SQL itself, returning number of rows modified:

print(Clickid.query
          .filter(Clickid.first_seen_at == None)
          .update({'first_seen_at': datetime.utcnow()})
          )
>>> 150

how do I make this work properly?

0

There are 0 answers