Skip to content

Commit

Permalink
fix selectable columns warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kvesteri committed Apr 9, 2021
1 parent 400f23a commit 202a644
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion sqlalchemy_utils/functions/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,10 @@ def get_columns(mixed):
instance or an alias of any of these objects
"""
if isinstance(mixed, sa.sql.selectable.Selectable):
return mixed.c
try:
return mixed.selected_columns
except AttributeError: # SQLAlchemy <1.4
return mixed.c
if isinstance(mixed, sa.orm.util.AliasedClass):
return sa.inspect(mixed).mapper.columns
if isinstance(mixed, sa.orm.Mapper):
Expand Down
8 changes: 5 additions & 3 deletions sqlalchemy_utils/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from sqlalchemy.ext import compiler
from sqlalchemy.schema import DDLElement, PrimaryKeyConstraint

from sqlalchemy_utils.functions import get_columns


class CreateView(DDLElement):
def __init__(self, name, selectable, materialized=False):
Expand Down Expand Up @@ -55,13 +57,13 @@ def create_table_from_selectable(
key=aliases.get(c.name, c.name),
primary_key=c.primary_key
)
for c in selectable.c
for c in get_columns(selectable)
] + indexes
table = sa.Table(name, metadata, *args)

if not any([c.primary_key for c in selectable.c]):
if not any([c.primary_key for c in get_columns(selectable)]):
table.append_constraint(
PrimaryKeyConstraint(*[c.name for c in selectable.c])
PrimaryKeyConstraint(*[c.name for c in get_columns(selectable)])
)
return table

Expand Down

0 comments on commit 202a644

Please sign in to comment.