Last active
May 13, 2023 11:11
-
-
Save jbking/5010252 to your computer and use it in GitHub Desktop.
How to get engine specific creating table DDL in SQLAlchemy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: from sqlalchemy import create_engine | |
In [2]: postgresql_engine = create_engine('postgresql://') | |
In [3]: mysql_engine = create_engine('mysql://') | |
In [4]: from myapp.resources import Event | |
In [5]: from sqlalchemy.schema import CreateTable | |
In [6]: print CreateTable(Event.__table__).compile(postgresql_engine) | |
CREATE TABLE events ( | |
id INTEGER NOT NULL, | |
uid VARCHAR(255) NOT NULL, | |
sequence INTEGER NOT NULL, | |
dtstart TIMESTAMP WITH TIME ZONE, | |
dtend TIMESTAMP WITH TIME ZONE, | |
body TEXT, | |
PRIMARY KEY (id), | |
FOREIGN KEY(id) REFERENCES contents (id), | |
UNIQUE (uid) | |
) | |
In [7]: print CreateTable(Event.__table__).compile(mysql_engine) | |
CREATE TABLE events ( | |
id INTEGER NOT NULL, | |
uid VARCHAR(255) NOT NULL, | |
sequence INTEGER NOT NULL, | |
dtstart DATETIME, | |
dtend DATETIME, | |
body TEXT, | |
PRIMARY KEY (id), | |
FOREIGN KEY(id) REFERENCES contents (id), | |
UNIQUE (uid) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment