Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jbking
Last active May 13, 2023 11:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbking/5010252 to your computer and use it in GitHub Desktop.
Save jbking/5010252 to your computer and use it in GitHub Desktop.
How to get engine specific creating table DDL in SQLAlchemy.
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