NiklasUllmann
NiklasUllmann

Reputation: 77

Export SQL Script from SQLAlchemy

I am using SQLAlchemy with ORM and DeclarativeMeta to connect to my Database.

Is there a way to generate or export a .sql file that contains all the Create Tables Commands?

Thank you!

I tried to get that information from my Meta Object or even from my SQLAlchemy Engine but they don't hold information like that.

Even the Meta.metadate._create_all() does not return a string or something else

Upvotes: 1

Views: 619

Answers (1)

NiklasUllmann
NiklasUllmann

Reputation: 77

Found an answers in the documentation of sqlalchemy.

from sqlalchemy.schema import CreateTable
print(CreateTable(my_mysql_table).compile(mysql_engine))

CREATE TABLE my_table (
id INTEGER(11) NOT NULL AUTO_INCREMENT,
...
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

SQLAlchemy Documentation!

Upvotes: 1

Related Questions