当我在下面的代码之后运行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))
)
答案 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)
)