M2M field not showing in return result #692
Unanswered
chad-szymarek
asked this question in
Q&A
Replies: 1 comment 9 replies
|
Strange. I created this demo script to make sure it works OK: import asyncio
import random
from piccolo.columns import Varchar, LazyTableReference, ForeignKey
from piccolo.columns.m2m import M2M
from piccolo.engine import SQLiteEngine
from piccolo.table import Table, create_db_tables, drop_db_tables
DB = SQLiteEngine()
class Band(Table, db=DB):
name = Varchar()
genres = M2M(joining_table=LazyTableReference(table_class_name='BandToGenre', module_path=__name__))
class Genre(Table, db=DB):
name = Varchar()
class BandToGenre(Table, db=DB):
band = ForeignKey(Band)
genre = ForeignKey(Genre)
async def main():
await drop_db_tables(Band, Genre, BandToGenre)
await create_db_tables(Band, Genre, BandToGenre)
genres = []
for genre_name in ['Rock', 'Country', 'Electronic']:
genre = await Genre.objects().create(name=genre_name)
genres.append(genre)
for band_name in ['Band_1', 'Band_2', 'Band_3']:
band = await Band.objects().create(name=band_name)
await band.add_m2m(random.choice(genres), m2m=Band.genres)
print(
await Band.select(Band.name, Band.genres())
)
if __name__ == '__main__':
asyncio.run(main())Is there definitely data in the joining table? |
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm trying to get a many to many field to work, and for some reason it doesn't seem to be working.
Here are my tables-
Here's my route-
I'm trying to have the
ActivitiesLearningactivitiestable, have adaysm2mfield with theProgramsDaytable. I made the joining table,LearningActivityToDaylike how it was in the docs I think. But when I try to do my select statement, to show the days in theActivitiesLearningactivitytable, that column just doesn't show up at all. No errors or anything. So I'm not quite sure what I did wrong.All reactions