sybase: can't wrap db table into Table object if the table doesn't contain primary key

Issue #3508 resolved
Eugene Zapolsky created an issue

Detailed Description:

When I try to wrap sybase table w/o primary key into sqlalchemy's Table object, I get the following exception.

Traceback (most recent call last):
..................
    cur_table = Table(record['tablename'], self.metadata, autoload=True)
  File "sqlalchemy/sql/schema.py", line 416, in __new__
    metadata._remove_table(name, schema)
  File "sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "sqlalchemy/sql/schema.py", line 411, in __new__
    table._init(name, metadata, *args, **kw)
  File "sqlalchemy/sql/schema.py", line 484, in _init
    self._autoload(metadata, autoload_with, include_columns)
  File "sqlalchemy/sql/schema.py", line 508, in _autoload
    self, include_columns, exclude_columns
  File "sqlalchemy/engine/base.py", line 1968, in run_callable
    return conn.run_callable(callable_, *args, **kwargs)
  File "sqlalchemy/engine/base.py", line 1477, in run_callable
    return callable_(self, *args, **kwargs)
  File "sqlalchemy/engine/default.py", line 364, in reflecttable
    return insp.reflecttable(table, include_columns, exclude_columns)
  File "sqlalchemy/engine/reflection.py", line 574, in reflecttable
    table_name, schema, table, cols_by_orig_name, exclude_columns)
  File "sqlalchemy/engine/reflection.py", line 647, in _reflect_pk
    table_name, schema, **table.dialect_kwargs)
  File "sqlalchemy/engine/reflection.py", line 412, in get_pk_constraint
    **kw)
  File "<string>", line 2, in get_pk_constraint
  File "sqlalchemy/engine/reflection.py", line 54, in cache
    ret = fn(self, con, *args, **kw)
  File "sqlalchemy/dialects/sybase/base.py", line 743, in get_pk_constraint
    for i in range(1, pks["count"] + 1):
TypeError: 'NoneType' object is not subscriptable

My quick and dirty fix is:

743,746c743,750
<         for i in range(1, pks["count"] + 1):
<             constrained_columns.append(pks["pk_%i" % (i,)])
<         return {"constrained_columns": constrained_columns,
<                 "name": pks["name"]}
---
>         if pks:
>             for i in range(1, pks["count"] + 1):
>                 constrained_columns.append(pks["pk_%i" % (i,)])
>             return {"constrained_columns": constrained_columns,
>                     "name": pks["name"]}
>         else:
>             return {"constrained_columns": [],
>                     "name": None}

Comments (5)

  1. Mike Bayer repo owner
    • Fixed two issues regarding Sybase reflection, allowing tables without primary keys to be reflected as well as ensured that a SQL statement involved in foreign key detection is pre-fetched up front to avoid driver issues upon nested queries. Fixes here courtesy Eugene Zapolsky; note that we cannot currently test Sybase to locally verify these changes. fixes #3508 fixes #3509

    → <<cset a7d7941d3eba>>

  2. Mike Bayer repo owner
    • Fixed two issues regarding Sybase reflection, allowing tables without primary keys to be reflected as well as ensured that a SQL statement involved in foreign key detection is pre-fetched up front to avoid driver issues upon nested queries. Fixes here courtesy Eugene Zapolsky; note that we cannot currently test Sybase to locally verify these changes. fixes #3508 fixes #3509

    (cherry picked from commit a7d7941d3ebafd16f603785c4677e371c675d1c0)

    → <<cset 90327f6a9369>>

  3. Mike Bayer repo owner

    thank you, note I had to merge these as-is without any testing. Please test the latest master and /or rel_1_0 branch! thanks.

  4. Log in to comment