.. changelog::
:version: 0.9.0b2
+ .. change::
+ :tags: bug, postgresql
+ :tickets: 2878
+
+ Fixed bug where values within an ENUM weren't escaped for single
+ quote signs. Note that this is backwards-incompatible for existing
+ workarounds that manually escape the single quotes.
+
+ .. seealso::
+
+ :ref:`migration_2878`
+
.. change::
:tags: bug, orm, declarative
:ticket:`2848`
+.. _migration_2878:
+
+Postgresql CREATE TYPE <x> AS ENUM now applies quoting to values
+----------------------------------------------------------------
+
+The :class:`.postgresql.ENUM` type will now apply escaping to single quote
+signs within the enumerated values::
+
+ >>> from sqlalchemy.dialects import postgresql
+ >>> type = postgresql.ENUM('one', 'two', "three's", name="myenum")
+ >>> from sqlalchemy.dialects.postgresql import base
+ >>> print base.CreateEnumType(type).compile(dialect=postgresql.dialect())
+ CREATE TYPE myenum AS ENUM ('one','two','three''s')
+
+Existing workarounds which already escape single quote signs will need to be
+modified, else they will now double-escape.
+
+:ticket:`2878`
New Features
============
return "CREATE TYPE %s AS ENUM (%s)" % (
self.preparer.format_type(type_),
- ",".join("'%s'" % e for e in type_.enums)
+ ", ".join(
+ self.sql_compiler.process(sql.literal(e), literal_binds=True)
+ for e in type_.enums)
)
def visit_drop_enum_type(self, drop):
from sqlalchemy.dialects.postgresql import TSRANGE
from sqlalchemy.orm import mapper, aliased, Session
from sqlalchemy.sql import table, column, operators
+from sqlalchemy.util import u
class SequenceTest(fixtures.TestBase, AssertsCompiledSQL):
'AS length_1', dialect=dialect)
+ def test_create_enum(self):
+ # test escaping and unicode within CREATE TYPE for ENUM
+ typ = postgresql.ENUM(
+ "val1", "val2", "val's 3", u('méil'), name="myname")
+ self.assert_compile(postgresql.CreateEnumType(typ),
+ u("CREATE TYPE myname AS ENUM ('val1', 'val2', 'val''s 3', 'méil')")
+ )
+
+ typ = postgresql.ENUM(
+ "val1", "val2", "val's 3", name="PleaseQuoteMe")
+ self.assert_compile(postgresql.CreateEnumType(typ),
+ "CREATE TYPE \"PleaseQuoteMe\" AS ENUM "
+ "('val1', 'val2', 'val''s 3')"
+ )
+
def test_create_partial_index(self):
m = MetaData()
tbl = Table('testtbl', m, Column('data', Integer))