1. idank
  2. sqlalchemy

Commits

Mike Bayer  committed 8963b06

fixes half of [ticket:192], query.load()/get() with a unicode argument was failing to apply type conversion to the bind parameter

  • Participants
  • Parent commits 525553f
  • Branches default

Comments (0)

Files changed (2)

File lib/sqlalchemy/orm/query.py

View file
  • Ignore whitespace
         if not hasattr(mapper, '_get_clause'):
             _get_clause = sql.and_()
             for primary_key in self.mapper.pks_by_table[self.table]:
-                _get_clause.clauses.append(primary_key == sql.bindparam("pk_"+primary_key._label))
+                _get_clause.clauses.append(primary_key == sql.bindparam("pk_"+primary_key._label, type=primary_key.type))
             self.mapper._get_clause = _get_clause
         self._get_clause = self.mapper._get_clause
     def _get_session(self):

File test/mapper.py

View file
  • Ignore whitespace
         u2 = s.get(User, 7)
         self.assert_(u is not u2)
 
+    def testunicodeget(self):
+        """tests that Query.get properly sets up the type for the bind parameter.  using unicode would normally fail 
+        on postgres, mysql and oracle unless it is converted to an encoded string"""
+        table = Table('foo', db, 
+            Column('id', Unicode(10), primary_key=True),
+            Column('data', Unicode(40)))
+        try:
+            table.create()
+            class LocalFoo(object):pass
+            mapper(LocalFoo, table)
+            crit = 'petit voix m\xe2\x80\x99a '.decode('utf-8')
+            print repr(crit)
+            create_session().query(LocalFoo).get(crit)
+        finally:
+            table.drop()
+
     def testrefresh(self):
         mapper(User, users, properties={'addresses':relation(mapper(Address, addresses))})
         s = create_session()