piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

get_m2m and extra_column_values

Open powellnorma opened this issue 3 years ago • 2 comments

After having inserted extra data to the joining table via the extra_column_values argument to add_m2m, how can I get those columns when using get_m2m. If not currently possible, it would be great if this would be added in the future.

powellnorma avatar Nov 13 '22 00:11 powellnorma

The best way to do this currently is to query the joining table.

If we have this schema:

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

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

class GenreToBand(Table):
    band = ForeignKey(Band)
    genre = ForeignKey(Genre)
    created_on = Timestamp()  # an extra column on the joining table

We can query it like this:

>>> await GenreToBand.select(
...     GenreToBand.created_on,  # an extra column on the joining table
...     GenreToBand.band.all_columns(),
...     GenreToBand.genre.all_columns()
... )

We might be able to modify get_m2m to return the extra column values - just not sure on what API would be best at the moment.

dantownsend avatar Nov 13 '22 16:11 dantownsend

It would be great if this functionality appeared. It would be nice to see this in select too.

madgagarin avatar May 17 '24 16:05 madgagarin