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
46
votes
8 answers

Alembic --autogenerate producing empty migration

I am trying to use Alembic for the first time and want to use --autogenerate feature described here My project structure looks like project/ configuration/ __init__.py dev.py …
daydreamer
  • 87,243
  • 191
  • 450
  • 722
40
votes
5 answers

Is there any way to generate sequential Revision IDs in Alembic?

I'm using Alembic as a database migration tool for a Python project. When I run a command like this: alembic revision -m "adding a column" ...it will add a new file called alembic/versions/xxxxxxxxxxxx_adding_a_column.py where xxxxxxxxxxxx is a…
soapergem
  • 9,263
  • 18
  • 96
  • 152
40
votes
3 answers

Alter Primary Key in Alembic?

I've read through the docs, but I can't find instructions on this anywhere. I tried dropping the old key and adding a new one, but that gets me errors: op.drop_constraint('PRIMARY', 'some_table', type_='primary') op.create_primary_key('PRIMARY',…
Eli
  • 36,793
  • 40
  • 144
  • 207
40
votes
2 answers

Can Alembic Autogenerate column alterations?

I was able to use alembic --autogenerate for when adding / removing columns. However, when I wanted to modify for example a "url" column from 200 characters to 2000 characters, it doesn't detect the change. How can I make Alembic (using…
Dexter
  • 6,170
  • 18
  • 74
  • 101
37
votes
1 answer

Is there a simple way to tell alembic to migrate to a specific revision?

Alembic has commands to upgrade and downgrade to a specific revision, e.g. on the command line: alembic upgrade And alembic downgrade Is there a simple way to migrate to a specific revision if you don't know…
Matt
  • 2,153
  • 1
  • 18
  • 29
36
votes
3 answers

Fetch table values using alembic and update to another table.

I have oauth secret and oauth key in client table. Now I moving them to oauth credentials table which will be created during migration. Alembic produced following schema for upgrade. from myapp.models import Client, ClientCredential from alembic…
Kracekumar
  • 19,457
  • 10
  • 47
  • 56
33
votes
4 answers

How to use Enum with SQLAlchemy and Alembic?

Here's my Post model: class Post(Base): __tablename__ = 'posts' title = db.Column(db.String(120), nullable=False) description = db.Column(db.String(2048), nullable=False) I'd like to add Enum status to it. So, I've created a new…
f1nn
  • 6,989
  • 24
  • 69
  • 92
33
votes
4 answers

How to import the own model into myproject/alembic/env.py?

I want to use alembic revision --autogenerate with my own model classes. Because of that I need to import them in myproject/alembic/env.py as described in the docs. But this doesn't work even if I tried a lot of variations. I am not sure in which…
buhtz
  • 10,774
  • 18
  • 76
  • 149
32
votes
3 answers

Alembic: How to add unique constraint to existing column

I have a table 'test' having a column 'Name' with no constraints. I need to ALTER this column by giving it a UNIQUE constraint. How should I do it? Should I use op.alter_column('???') or create_unique_constraint('???')? Isn't…
Ishwar
  • 439
  • 2
  • 6
  • 10
31
votes
2 answers

Creating "zero state" migration for existing db with sqlalchemy/alembic and "faking" zero migration for that existing db

I want to add alembic to an existing ,sqlalchemy using, project, with a working production db. I fail to find what's the standard way to do a "zero" migration == the migration setting up the db as it is now (For new developers setting up their…
alonisser
  • 11,542
  • 21
  • 85
  • 139
30
votes
3 answers

Alembic - sqlalchemy does not detect existing tables

I've asked a question (Alembic - sqlalchemy initial migration) on how to detect tables by using target_metadata = Base.metadata for alembic revision --autogenerate -m "initial migration" After I've imported my models to env.py file it seemed to…
Efrin
  • 2,323
  • 3
  • 25
  • 45
30
votes
4 answers

Using the SQLAlchemy ORM inside an Alembic migration: how do I?

I currently have a column that contains HTML markup. Inside that markup, there is a timestamp that I want to store in a new column (so I can query against it). My idea was to do the following in a single migration: Create a new, nullable column for…
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
29
votes
4 answers

Alembic: alembic revision says Import Error

I am trying to integrate my Flask project with Alembic My application structure looks like project/ configuration/ __init__.py dev.py test.py core/ # all source…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
28
votes
2 answers

Update column content during Alembic migration

Suppose my db model contains an object User: Base = declarative_base() class User(Base): __tablename__ = 'users' id =…
Jens
  • 8,423
  • 9
  • 58
  • 78
27
votes
5 answers

Flask-migrate and changing column type

I am trying to learn some Flask and I am using Flask-Migrate 1.6.0 So I made a model which looks like this class Download(db.Model): __tablename__ = "downloads" id = db.Column(db.Integer, autoincrement=True, primary_key=True) filename…
Thustra
  • 319
  • 1
  • 3
  • 8
1
2
3
56 57