Given an Alembic migration, would you recommend the following code to 
rename constraints?

from alembic import op                                                     
                         
import sqlalchemy as sa                                                     
                        
from srv.orm.meta import NAMING_CONVENTION # as per Pylons cookiecutter 
template                                                         
                                                                            
                        
def upgrade():                                                             
                         
                                                                            
                        
    connection = op.get_bind()                                             
                         
    engine = connection.engine                                             
                         
    metadata = sa.MetaData(naming_convention=NAMING_CONVENTION)             
                        
                                                                            
                        
    for table_name in engine.table_names():                                 
                        
        table = sa.Table(table_name, metadata, autoload_with=connection)   
                         
        for fk in table.foreign_keys:                                       
                        
            op.drop_constraint(fk.name, table_name, type_="foreignkey")     
                       
            fk.name = None                                                 
                        
            op.invoke(CreateForeignKeyOp.from_constraint(fk))               
                       

For downgrade() we'd create a metadata without naming_convention, thus 
falling back to the db's naming (which is where we're coming from).

However, how would I go about iterating over indexes, unique constraints, 
and check constraints of a table?

Thank you!


On Wednesday, August 30, 2017 at 7:20:32 AM UTC+10, jens.t...@gmail.com 
wrote:
>
> Thank you, Mike!
>
> I’ll take a closer look at your proposed code this week.
>
> I am curious though: not even MySQL has a rename feature 
> <https://stackoverflow.com/questions/6188011/how-do-i-rename-a-foreign-key-in-mysql#13782962>,
>  
> is that because of consistency? PostgreSQL adds ALTER TABLE … RENAME 
> CONSTRAINT 
> <https://www.postgresql.org/docs/9.2/static/sql-altertable.html> with 9.2 
> though. (Boy, I keep running into issues that keep pushing me towards 
> Porstgres.)
>
> Jens
>
 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to