28

I am developing a small registration application for a friend zumba class, using Flask, SQLAlchemy and Flask-migrate(alembic) to deal with db update. I settled on SQlite because the application has to be self contained and runs locally on a laptop without internet access and SQLite requires no installation of a service or other, which is a must too.

Dealing with SQLite lack of support of ALTER table wasn't a problem during the initial development as I simply destroyed, recreated the DB when that problem arised. But now that my friend is actually using the application I am facing a problem.

Following a feature request a table has to be modified and once again I get the dreaded " "No support for ALTER of constraints in SQLite dialect". I foresee that this problem will probably arise in the future too.

How can I deal with this problem? I am pretty much a newbie when it comes to dealing with database. I read that a way to deal with that is to create a new table, create the new constraint and copy the data and rename the table, but I have no idea how to implement that in the alembic script.

1

2 Answers 2

40

You can set a variable (render_as_batch=True) in the env.py file created with the initial migration for both online and offline migrations.

context.configure(
    connection=connection,
    target_metadata=target_metadata,
    render_as_batch=True
)

It requires alembic>0.7.0

This enables generation of batch operation migrations, i.e. creates a new table with the constraint, copies the existing data over, and removes the old table. See https://alembic.sqlalchemy.org/en/latest/batch.html#batch-mode-with-autogenerate

If you still encounter issues, be advised - there is still nuance with sqlite, e.g. https://alembic.sqlalchemy.org/en/latest/batch.html#dropping-unnamed-or-named-foreign-key-constraints

2
  • 2
    Thanks for the references. The links above are now broken but the page is available at alembic.zzzcomputing.com/en/latest/batch.html
    – xolox
    Commented Jul 13, 2016 at 11:27
  • 1
    thanks for the updated link @xolox I'd like to make sure it's readily avaible for anyone else with this question is customary for you to edit the original answer with the updated link? Commented Oct 12, 2016 at 18:26
1

adding to the above answer: add render_as_batch=True to both offline and online

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.