Questions tagged [alembic]

Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python.

Alembic is a lightweight database migration tool for usage with the SQLAlchemy Database Toolkit for Python.

Some of the important operations that can be performed with alembic are

  • Upgrade(Perform Database migrations, Example: create/alter/modify)
  • Downgrade(Its the reverse operation of upgrade to undo the upgrade changes)
  • Current(Show the current revision and state of migration in the database)
  • Init(Initialize a new scripts directory)
  • Revision(Create a new revision file)

Related tags

References

854 questions
165
votes
3 answers

How do I execute inserts and updates in an Alembic upgrade script?

I need to alter data during an Alembic upgrade. I currently have a 'players' table in a first revision: def upgrade(): op.create_table('player', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name',…
Arek S
  • 4,239
  • 4
  • 24
  • 33
163
votes
12 answers

Target database is not up to date

I'd like to make a migration for a Flask app. I am using Alembic. However, I receive the following error. Target database is not up to date. Online, I read that it has something to do with…
GangstaGraham
  • 8,865
  • 12
  • 42
  • 60
120
votes
14 answers

Is it possible to store the alembic connect string outside of alembic.ini?

I'm using Alembic with SQLAlchemy. With SQLAlchemy, I tend to follow a pattern where I don't store the connect string with the versioned code. Instead I have file secret.py that contains any confidential information. I throw this filename in my…
Doug T.
  • 64,223
  • 27
  • 138
  • 202
95
votes
3 answers

Undo last Alembic migration

I created a migration with alembic revision --autogenerate, applied it to my development database with alembic upgrade head, and then realised it wasn't quite what I wanted. How can I revert the migration so that I can tweak it and try again?
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
81
votes
4 answers

Alembic: IntegrityError: "column contains null values" when adding non-nullable column

I'm adding a column to an existing table. This new column is nullable=False. op.add_column('mytable', sa.Column('mycolumn', sa.String(), nullable=False)) When I run the migration, it complains: sqlalchemy.exc.IntegrityError: column "mycolumn"…
Ron
  • 7,588
  • 11
  • 38
  • 42
81
votes
16 answers

Altering an Enum field using Alembic

How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I'm not quite sure how best to translate…
bboe
  • 4,092
  • 3
  • 29
  • 39
78
votes
9 answers

Using Alembic API from inside application code

I am using SQLite as an application file format (see here for why you would want to do this) for my PySide-based desktop application. That is, when a user uses my app, their data is saved in a single database file on their machine. I am using the…
John David Reaver
  • 1,238
  • 1
  • 10
  • 17
73
votes
4 answers

alembic util command error can't find identifier

I'm trying to use alembic to handle local migrations on my project. It worked the first time, but then I needed to delete the folder and restart.(don't ask why, I just had to) I'm following this tutorial and I run the command python manage.py db…
65
votes
5 answers

Creating seed data in a flask-migrate or alembic migration

How can I insert some seed data in my first migration? If the migration is not the best place for this, then what is the best practice? """empty message Revision ID: 384cfaaaa0be Revises: None Create Date: 2013-10-11 16:36:34.696069 """ #…
Mark Richman
  • 28,948
  • 25
  • 99
  • 159
63
votes
4 answers

alembic revision - multiple heads (due branching) error

I've got an application and I wanted to create a new migration for it today. When I run $ alembic revision -m "__name__" I got a message Only a single head is supported. The script directory has multiple heads (due branching), which must be…
Arek S
  • 4,239
  • 4
  • 24
  • 33
57
votes
11 answers

sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver

I am trying to run alembic migration and when I run alembic revision --autogenerate -m "Added initial tables" It fails saying sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver the database url is…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
56
votes
4 answers

.ini file load environment variable

I am using Alembic for migrations implementation in a Flask project. There is a alembic.ini file where the database configs must be specified: sqlalchemy.url = driver://user:password@host/dbname Is there a way to specify the parameters from the…
dimmg
  • 3,316
  • 2
  • 20
  • 29
53
votes
2 answers

How to use alter_column in alembic?

I am writing a migration in alembic but seems impossible to me the change the value of server_defaults from something to nothing. My code: op.alter_column("foo", sa.Column("bar", sa.DateTime(timezone=False), server_default=None, nullable=True)) If…
tapioco123
  • 3,235
  • 10
  • 36
  • 42
52
votes
1 answer

sqlalchemy : executing raw sql with parameter bindings

I'm trying to run this simple raw sql statement with parameters with SQLALchemy (within an alembic script) : from alembic import op t = {"code": "123", "description": "one two three"} op.execute("insert into field_tags (id, field_id, code,…
Max L.
  • 9,774
  • 15
  • 56
  • 86
48
votes
2 answers

Alembic: alter column type with USING

I'm attempting to use alembic to convert a SQLAlchemy PostgreSQL ARRAY(Text) field to a BIT(varying=True) field for one of my table columns. The column is currently defined as: cols = Column(ARRAY(TEXT), nullable=False, index=True) I want to change…
bard
  • 2,762
  • 9
  • 35
  • 49
1
2 3
56 57