Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M2M and all_columns #728

Closed
powellnorma opened this issue Dec 18, 2022 · 2 comments · Fixed by #730
Closed

M2M and all_columns #728

powellnorma opened this issue Dec 18, 2022 · 2 comments · Fixed by #730

Comments

@powellnorma
Copy link

When using all_columns() in a M2M Select like this:

from piccolo.columns.column_types import Varchar, LazyTableReference, ForeignKey
from piccolo.columns.m2m import M2M
from piccolo.engine.postgres import PostgresEngine
from piccolo.table import Table
from starlette.applications import Starlette
import uvicorn

DB = PostgresEngine(config={
    'host': 'localhost',
    'database': 'test',
    'user': 'test',
    'password': ''
})

class Band(Table):
    name = Varchar()
    genres = M2M(LazyTableReference("GenreToBand", module_path=__name__))

class Genre(Table):
    name = Varchar()
    bands = M2M(LazyTableReference("GenreToBand", module_path=__name__))

# This is our joining table:
class GenreToBand(Table):
    band = ForeignKey(Band)
    genre = ForeignKey(Genre)

async def on_startup():
    await Band.select(Band.name, Band.genres(Genre.all_columns(), as_list=True))

app = Starlette(debug=True, on_startup=[on_startup])

if __name__ == '__main__':
    uvicorn.run(app, host='127.0.0.1', port=64215)

I get:

  [..]
  File "/piccolo/piccolo/columns/m2m.py", line 53, in <genexpr>
    (column.__class__.value_type in safe_types)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'list' has no attribute 'value_type'
@powellnorma
Copy link
Author

The same happens when selecting a ReverseLookup field (from #599) in a M2M Query (or vice versa)

@dantownsend
Copy link
Member

I think Band.genres(Genre.all_columns(), as_list=True) isn't unpacking Genre.all_columns() automatically.

This should work:

Band.genres(*Genre.all_columns(), as_list=True)

It just needs a small tweak here:

def __call__(
self, *columns: Column, as_list: bool = False, load_json: bool = False
) -> M2MSelect:

To do something like this:

_columns = []
for column in columns:
if isinstance(column, list):
_columns.extend(column)
else:
_columns.append(column)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants