Column Rename Downgrade Woe

18 views
Skip to first unread message

Jerry

unread,
Dec 20, 2010, 11:54:42 AM12/20/10
to migrate-users
I have the following migrate script mainly for renaming a column --

"""
from sqlalchemy import *
from migrate import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
__tablename__ = 'users'
user_id = Column(Unicode(32), primary_key=True) # UUID 32 hex
full_name = Column(Unicode(128), index=True) # !

display_name = Column(Unicode(32), index=True) # !

def upgrade(migrate_engine):
Base.metadata.bind = migrate_engine
Base.metadata.tables[User.__tablename__].c.full_name.alter(
name='display_name', type=Unicode(32))

def downgrade(migrate_engine):
Base.metadata.bind = migrate_engine
alter_column(display_name,
table=Base.metadata.tables[User.__tablename__],
name='full_name', type=Unicode(128))
"""

The upgrade works fine, however, I couldn't for my life figure out how
to make the downgrade work because by the time it runs, the column
already bears the new name, which prevents me from using
table.c.full_name.alter(...)

Exception --

File "/home/jerryj/virtualenv/lib/python2.6/site-packages/
SQLAlchemy-0.6.5-py2.6.egg/sqlalchemy/sql/compiler.py", line 1529, in
_requires_quotes
lc_value = value.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

More trace back at http://pastebin.com/4NJVqmMz

Any tip will be much appreciated!

Jerry

Jerry

unread,
Dec 21, 2010, 2:46:36 PM12/21/10
to migrate-users
Hi,

Just to answer myself, I finally managed to get away with this --

"""
def upgrade(migrate_engine):
class User(Base):
__tablename__ = 'users'
__table_args__ = {'useexisting': True}
user_id = Column(Unicode(32), primary_key=True) # UUID 32 hex
full_name = Column(Unicode(128), index=True) # !

Base.metadata.bind = migrate_engine
Base.metadata.tables[User.__tablename__].c.full_name.alter(
name='display_name', type=Unicode(32))

def downgrade(migrate_engine):
class User(Base):
__tablename__ = 'users'
__table_args__ = {'useexisting': True} # for migrate test when
downgrade runs immediately after upgrade
user_id = Column(Unicode(32), primary_key=True) # UUID 32 hex
display_name = Column(Unicode(32), index=True) # !

Base.metadata.bind = migrate_engine

Base.metadata.tables[User.__tablename__].c.display_name.alter(name='full_name',
type=Unicode(128))
"""

But I have to repeat User class definition, there are most likely
better ways.

Jerry
> More trace back athttp://pastebin.com/4NJVqmMz

Nate Lowrie

unread,
Dec 21, 2010, 6:55:59 PM12/21/10
to migrat...@googlegroups.com
I always have upgrade and downgrade definitions of the table for
column renames because you are altering the schema definition of the
table and it's technically a different table. You solution is about
the cleanest there is.

Regards,

Nate

> --
> You received this message because you are subscribed to the Google Groups "migrate-users" group.
> To post to this group, send email to migrat...@googlegroups.com.
> To unsubscribe from this group, send email to migrate-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/migrate-users?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages