CreateTable doesn't specify AUTO INCREMENT or indexes
Is CreateTable
the best way to get the SQL syntax for table creation?
If I run my program altogether, I see it in the echoed SQL output, but it seems like there has to be a better way. I'm at a loss for how to get AUTO INCREMENT
sequences to print along with table creation syntax.
>>> table = MyModel.__table__
>>> print sqlalchemy.schema.CreateTable(table)
This will print something like:
CREATE TABLE table_name (
sequence INTEGER NOT NULL,
id VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
status_message TEXT,
PRIMARY KEY (sequence)
)
It doesn't close the SQL statement with a semicolon. To get indexes/indices, I must write:
for index in table.indexes:
print sqlalchemy.schema.CreateIndex(index)
To get:
CREATE INDEX ix_table_name_id ON table_name (id)
CREATE INDEX ix_table_name_status ON table_name (status)
Now, compare the above to the echoed SQL output:
2014-12-22 09:10:58,697 INFO sqlalchemy.engine.base.Engine
CREATE TABLE table_name (
sequence INTEGER NOT NULL AUTO_INCREMENT,
id VARCHAR(128) NOT NULL,
status VARCHAR(128) NOT NULL,
status_message TEXT,
PRIMARY KEY (sequence),
)
2014-12-22 09:10:58,723 INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_table_name_id ON table_name (id)
2014-12-22 09:10:58,744 INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_table_name_status ON table_name (status)
Comments (3)
-
repo owner -
repo owner - changed status to closed
these are usage issues, so please continue on the SQLAlchemy mailing list, thanks.
-
reporter Thanks @zzzeek for humoring me!
mysql.dialect()
(fromsqlalchemy.dialects
) got me theAUTO_INCREMENT
statement like you said it would.I had previously tried
CreateTable(table).compile(my_engine)
, but the lacked it, though maybe I did something wrong along the way. - Log in to comment
this is not needed for SQL execution. the semicolon is an artifact of your SQL shell, just append it.
indexes are separate CREATE statements which would need to be passed to execute() separately, so yes these are necessarily separate in this context. We are dealing with single statement at a time.
follow the guidelines in the FAQ (sorry, readthedocs seems to be down at the moment), search for "How can I get the CREATE TABLE/ DROP TABLE output as a string? " since it's down, here's the text:
that is,
CreateTable(table).compile(my_engine)
, orCreateTable(table).compile(dialect=mysql.dialect())
.