Skip to content Skip to sidebar Skip to footer

Flask-sqlalchemy How To Use Create_all With Schema_translate_map

The SQLAlchemy provides the Connection.execution_options.schema_translate_map for change the schemas in execution time, as said in docs. In the examples is shown how to use to perf

Solution 1:

When you set execution options, you create copy of connection. This mean what create_all run without schema_translate_map.

>>>c = Base.session.connection()>>>w = c.execution_options(schema_translate_map={'dynamic':'kek'})>>>c._execution_options
immutabledict({})
>>>w._execution_options
immutabledict({'schema_translate_map': {'dynamic': 'kek'}})

Solution 2:

to achieve your goal, you could try another approach.

get the tables from old grammmar and adapt for new metadata.

metadata = MetaData(bind=engine, schema=db_schema)
fortablein db.Model.metadata.tables.values():
    table.tometadata(metadata)
metadata.drop_all()
metadata.create_all()

Post a Comment for "Flask-sqlalchemy How To Use Create_all With Schema_translate_map"