How to rename constraints

54 views
Skip to first unread message

Jason R. Coombs

unread,
Jul 10, 2009, 10:48:48 AM7/10/09
to migrat...@googlegroups.com

I’d like to create a routine for renaming foreign key constraints in our database (one of our migration steps).  I ended up creating this routine, which works, but it’s a bit ugly, and I expect brittle: http://paste.turbogears.org/paste/94119.

 

This implementation is clumsy because, as I understand it,

(a)    There’s no way to query for a foreign key constraint by name.

(b)   There’s no straightforward way to get all foreign key constraints for a database.

(c)    A migrate.changeset.constraint.ForeignKeyConstraint must be constructed that matches the existing constraint.

(d)   There’s no easy way to construct a migrate.changeset.constraint.ForeignKeyConstraint from a sqlalchemy.schema.ForeignKeyConstraint.

(e)   Using the same ForeignKeyConstraint for the drop and create operation (only changing the name in between) fails (SQL syntax error with the child/local column list empty).

 

I’m hoping that I’m mistaken and that there’s a more elegant way to accomplish what I’m trying to do.

 

Is there something I’m missing that could help accomplish this seemingly simple operation?

 

Regards,

Jason

 

 

Domen Kožar

unread,
Jul 10, 2009, 11:54:07 AM7/10/09
to migrate-users
Hey Jason,

Your approach is considered a hack, since you make a lot of
assumptions. You always know what table does constraint belong to,
what columns does it reffer to and what is it's name.

You should gather that info before writing migrate script and just
initialize migrate's ForeignKey and create/drop it.

However, I agree that current API migrate provides for managing
Constraints is clumsy. I am willing to make the changes, but I clearly
don't know a perfect way how to approach this (we will have to break
backward compatiblity and thus it should be done for good)

About your rundown:

a) This is because constraint names are not unique across tables.
Reference it through table.
b) Explained in a)
c) 0.6.0 will change this behavior so that Constraints will be patched
to work with SA natively
d) guess c) will eliminate this
e) Is this reproducable with trunk? Can you write a simple testcase?

d.
>  smime.p7s
> 9KViewDownload
Reply all
Reply to author
Forward
0 new messages