Adam Morris
Adam Morris

Reputation: 35

Changing a defined column using sqlalchemy into a longer-length unicode

I have a table defined in my app like this:

users = Table('users', metadata,
    Column('user_id', Integer, autoincrement=True, primary_key=True),
    Column('user_name', Unicode(16), unique=True, nullable=False),
    Column('email_address', Unicode(255), unique=True, nullable=False),
    Column('display_name', Unicode(255)),
    Column('password', Unicode(80)),
    Column('created', DateTime, default=datetime.now),
    mysql_engine='InnoDB',
    mysql_charset='utf8',
)

However, after developing for a while, I want to change user_name to a longer length, such as Unicode(255). As I understand it, this definition is run at first start-up, and so just changing this line wouldn't work for existing databases. It needs to migrate to this new definition. How do I go about converting already created databases into the new, desired definition?

Upvotes: 1

Views: 2111

Answers (2)

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

Edit alembic/env.py and modify your configuration to include compare_type=True:

def run_migrations_online():
    ...
    with connectable.connect() as connection:
            context.configure(
                connection=connection,
                target_metadata=target_metadata,
                compare_type=True, # <------------- THIS LINE HERE
            )
    ...

Disclosure, I got this solution from this helpful guide.

Upvotes: 0

mickeybob
mickeybob

Reputation: 457

You are correct that updating your code will not update an existing database schema. You can use Alembic to auto-generate and run schema migrations. Alembic can auto-generate change scripts for you by comparing the schema of your newly edited metadata with the schema from your database. Start here: http://alembic.readthedocs.org/en/latest/

Upvotes: 1

Related Questions