Some of the unit-tests have parameters which don't refer to column
names so this is the latest formulation:

if identity_column in [t.c[key].name for key in
self.compiled_parameters[0] if hasattr(t.c, key)]:


On Mar 4, 5:34 pm, "phrrn...@googlemail.com" <phrrn...@googlemail.com>
wrote:
> It is common (for us) to have tables with an identity column and that
> column *not* be the primary key. I am dealing with this by querying
> the system catalogs and caching the result within the table meta-data
> object itself (probably a bad idea but doing it for expediency). The
> system catalog query returns -- of course -- the database-level
> identifier for the column with the identity property set. How do I
> find out if the insert parameters have a value for the identity
> column? I am using something like this which has a very evil look to
> it:
>
>  if identity_column in [t.c[key].name for key in
> self.compiled_parameters[0]]:
>
> pjjH
>
>         if self.compiled.isinsert:
>             self.logger.debug('pre_exec for an
> INSERT')
>             t = self.compiled.statement.table
>            if self._table_identity_column(t) is not None:
>                 self.HAS_IDENTITY = True
>                 # This returns the database-level name of the column
>                 # Since the parameters may have different names (this
>                 # will happen if a named parameter, 'key', was passed
>                 # to the Column constructor), we need to project out
> the names
>                 identity_column = self._table_identity_column(t)
>                 self.logger.debug([t.c[key].name for key in
> self.compiled_parameters[0]])
>                 if identity_column in [t.c[key].name for key in
> self.compiled_parameters[0]]:
>                     self.IDENTITY_INSERT = True
>                     self.logger.debug('setting IDENTITY_INSERT ON FOR
> %s' % (t))
>
> # detect if the table has an identity column by direct query against
> the system catalogs
>    def _table_identity_column(self, t):
>         """Return the name of the this table's identity column"""
>         # negative caching
>
>         if not hasattr(t, '_identity_column'):
>             t._identity_column = None
>             s = sql.select([syscolumns.c.name],
> from_obj=syscolumns.join(sysobjects))
>             s = s.where(sql.and_(sysobjects.c.name == t.name, sql.text
> ("(status & 128 = 128)")))
>             r = self.connection.execute(s).fetchone()
>             if r is not None:
>                 t._identity_column  = r[0]
>
        return t._identity_column
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to