azazel75/macropy

sql[(x for x in table)] should translate to table.select()

thedrow opened this issue · 7 comments

Right now when doing:

sql[(x for x in model)]

We get the following exception:

    def test_select_all(persistence_model):
>       query = sql[(x for x in persistence_model)]

tests/unit/relational/test_querying.py:5: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/unit/relational/test_querying.py:5: in <lambda>
    query = sql[(x for x in persistence_model)]
<string>:2: in select
    ???
../../../.local/share/virtualenvs/ostarm-GnF5T52P/lib/python3.6/site-packages/sqlalchemy/sql/selectable.py:2735: in __init__
    c = _interpret_as_column_or_from(c)
../../../.local/share/virtualenvs/ostarm-GnF5T52P/lib/python3.6/site-packages/sqlalchemy/sql/elements.py:4342: in _interpret_as_column_or_from
    if element == "*":
<string>:1: in <lambda>
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.sql.base.ImmutableColumnCollection object at 0x7f0a41332318>, elements = <sqlalchemy.util.langhelpers.dependencies._importlater object at 0x7f0a443a6b70>, other = '*'

    @util.dependencies("sqlalchemy.sql.elements")
    def __eq__(self, elements, other):
        l = []
        for c in getattr(other, "_all_columns", other):
            for local in self._all_columns:
>               if c.shares_lineage(local):
E               AttributeError: 'str' object has no attribute 'shares_lineage'

../../../.local/share/virtualenvs/ostarm-GnF5T52P/lib/python3.6/site-packages/sqlalchemy/sql/base.py:566: AttributeError

how It should distinguish between a Table or a model class? That's not really a bug, it's supposed to work with Table instances.

You can always contribute an enhancement or a new macro tha works with model classes though. :-)
I'm not using sqlalchemy so much at the moment

@thedrow sorry to bother, I'm giving it some time for fun, and I understood that with "model" in the title you meant "sqlalchemy declarative model class instances" like those contructed in this sqlalchemy tutorial and so I've tried building a declarative model for the "world" db used by pinq tests and to query an instance of one model class (the country) but it has no select() method, which "model" are you referring to? Please clarify.

I'm not talking about a declarative model. I'm talking about a Table object.
I apologize for my lack of clarity.

So, I'm right when I say that the various db.xx objects like db.Country in tests/pinq.py are Table instances? And if that's the case, what really is the problem?

That

sql[(x for x in db.Country)]

produces the error I provided.

Let me know if you have any other issue