jazzis18
jazzis18

Reputation: 213

SQLAlchemy: Update table structure

I created the table on the server using SQLALchemy:

from sqlalchemy import create_engine, Table, Column, String, MetaData

engine = create_engine('mssql://server/database?driver=SQL+Server&trusted_connection=yes')
meta = MetaData() 
table = Table('test17', meta,
    Column('id', Integer, primary_key=True),
    Column('name', String('255'))
)

metadata.create_all(engine)

Then I connected to this database using SSMS 2012 and added a new column:

ALTER TABLE test17 ADD age INT NULL

How do I tell using SQLALchemy that a new column appears in the table?

I tried to do something like:

meta2 = MetaData()
table = Table('test17', meta, autoload=True, autoload_with=engine)

But in the end I get the same table structure that I defined initially using SQLALchemy.

Upvotes: 0

Views: 4362

Answers (1)

c8999c 3f964f64
c8999c 3f964f64

Reputation: 1627

I think you forgot to bind your MetaData to the engine. Create the engine first, then read the metadata from the db using the engine.

import sqlalchemy as db
metadata = db.MetaData(bind=engine)
test17 = db.Table('test17', metadata, autoload=True)

Upvotes: 1

Related Questions