piccolo
piccolo copied to clipboard
get_m2m and extra_column_values
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.
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.
It would be great if this functionality appeared. It would be nice to see this in select too.