Skip to content Skip to sidebar Skip to footer

Sql Alchemy And Generating Alter Table Statements

I want to programatically generate ALTER TABLE statements in SQL Alchemy to add a new column to a table. The column to be added should take its definition from an existing mapped c

Solution 1:

After tracing engine.create_all() with a debugger I've discovered a possible answer:

>>>engine.dialect.ddl_compiler(...   engine.dialect,...   DBChain.__table__.c.rName ) \....get_column_specification(...   DBChain.__table__.c.rName )
'"rName" VARCHAR(40)'

The index can be created with:

sColumnElement = DBChain.__table__.c.rName
if sColumnElement.index:                                   
    sIndex = sa.schema.Index(                              
            "ix_%s_%s" % (rTableName, sColumnElement.name),
            sColumnElement,                                
            unique=sColumnElement.unique)                  
    sIndex.create(engine)                                

Solution 2:

You should migrate database content properly:

flask db stamp head
flask db migrate
flask db upgrade

Post a Comment for "Sql Alchemy And Generating Alter Table Statements"