bindparam name too long. Similar to #127

Issue #244 resolved
Former user created an issue

The name of bind parameter can get over 30 characters on occasion and Oracle does not like it. If label is already at 30 the source:/sqlalchemy/trunk/lib/sqlalchemy/orm/query.py line 29 will push it over the limit.

One could generate shorter label names in source:/sqlalchemy/trunk/lib/sqlalchemy/sql.py:

Index: sql.py

===================================================================

--- sql.py  (revision 1719)

+++ sql.py  (working copy)

@@ -1189,7 +1189,7 @@

             if self.table is not None and self.table.named_with_column():
                 self.__label =  self.table.name + "_" + self.name
                 if self.table.c.has_key(self.__label) or len(self.__label) >= 30:
-                    self.__label = self.__label[0:24](0:24) + "_" + hex(random.randint(0, 65535))[2:](2:)
+                    self.__label = self.__label[0:22](0:22) + "_" + hex(random.randint(0, 65535))[2:](2:)
             else:
                 self.__label = self.name
         return self.__label

or prepend something shorter that 'pk_':

Index: query.py

===================================================================

--- query.py    (revision 1719)

+++ query.py    (working copy)

@@ -26,7 +26,7 @@

         if not hasattr(self.mapper, '_get_clause'):
             _get_clause = sql.and_()
             for primary_key in self.mapper.pks_by_table[self.table](self.table):
-                _get_clause.clauses.append(primary_key == sql.bindparam("pk_"+primary_key._label, type=primary_key.type))
+                _get_clause.clauses.append(primary_key == sql.bindparam("p"+primary_key._label, type=primary_key.type))
             self.mapper._get_clause = _get_clause
         self._get_clause = self.mapper._get_clause
     def _get_session(self):
@@ -280,7 +280,7 @@

         i = 0
         params = {}
         for primary_key in self.mapper.pks_by_table[self.table](self.table):
-            params["pk_"+primary_key._label]("pk_"+primary_key._label) = ident[i](i)
+            params["p"+primary_key._label]("p"+primary_key._label) = ident[i](i)
             # if there are not enough elements in the given identifier, then 
             # use the previous identifier repeatedly.  this is a workaround for the issue 
             # in #185, where a mapper that uses joined table inheritance needs to specify

Comments (1)

  1. Mike Bayer repo owner

    the "pk_" prefix is superfluous; i removed it in changeset:1722.

    if you try out the trunk with oracle, be advised there is a change to the workings of foreign keys with regards to table creation which I havent tested out yet (is probably fine).

  2. Log in to comment