Hi:
It seems that the fact describes a procedure to get a string to create
a table:
http://docs.sqlalchemy.org/en/latest/faq.html#how-can-i-get-the-create-table-drop-table-output-as-a-string
However, this does not work correctly when a table has a
server_default which is a function that takes arguments:
tbl = Table("derp", metadata,
Column("arr", ARRAY(Text), server_default=func.array(["foo",
"bar", "baz"])),
)
When using the method described in the docs:
stmt = str(schema.CreateTable(tbl).compile(dialect=session.bind.dialect))
We get:
CREATE TABLE derp (
arr TEXT[] DEFAULT array(%(array_1)s)
)
The solution (for psycopg2, anyway):
stmt = schema.CreateTable(tbl).compile(dialect=session.bind.dialect)
dialect = session.bind.dialect
enc = dialect.encoding
params = {}
for k,v in stmt.sql_compiler.params.iteritems():
if isinstance(v, unicode):
v = v.encode(enc)
params[k] = sqlescape(v)
stmt = (str(stmt).encode(enc) % params).decode(enc)
Which results in the expected:
CREATE TABLE derp (
arr TEXT[] DEFAULT array(%(array_1)s)
)
Of course, there isn't a generic solution to this problem, but I think
the docs should describe the limitations of the given solution, at a
minimum.
-Ryan Kelly
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.