如何使用sqlalchemy打印CreateTable(表)

时间:2015-01-13 22:32:42

标签: python postgresql sqlalchemy

当我在下面的代码之后运行print CreateTable(question)时出现错误,这是因为它没有正确编译,因为ARRAY是postgres特有的。如何编译正确的CREATE脚本?我在哪里输入与方言相关的编译信息?

from sqlalchemy import Table, Column, Integer, String, ForeignKey, MetaData
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.schema import CreateTable

metadata=MetaData()

user = Table('user', metadata,
            Column('id', Integer, primary_key=True)
            )

question = Table('question', metadata,
            Column('id', Integer, primary_key=True),
            Column('description', String),
            Column('answers', ARRAY(String))
            )

1 个答案:

答案 0 :(得分:2)

from sqlalchemy.dialects import postgresql
print CreateTable(question).compile(dialect=postgresql.dialect())

CREATE TABLE question (
id SERIAL NOT NULL, 
description VARCHAR, 
answers VARCHAR[], 
PRIMARY KEY (id)
)