0

I have 2 classes. The class SimpleThing can also contain a list of itself as children.

class SimpleThing():

    __tablename__ = 'Simple_Thing'
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    bundled_things = db.relationship('SimpleThing',                
        secondary='Bundled_Things',
        primaryjoin="SimpleThing.id==Bundled_Thing.c.bundle_id",
        secondaryjoin="SimpleThing.id==Bundled_Thing.c.simple_id")

class BundledThing():

    __tablename__ = 'Bundled_Thing'

    bundle_fkey = db.ForeignKey('Simple_Thing.id')
    bundle_id = db.Column(db.Integer, bundle_fkey, nullable=False)
    bundle_thing = db.relationship('SimpleThing',
        foreign_keys='BundledThing.bundle_id')

    simple_fkey = db.ForeignKey('Simple_Thing.id')
    simple_id = db.Column(db.Integer, simple_fkey, nullable=False)
    simple_thing = db.relationship('SimpleThing',
        foreign_keys='BundledThing.simple_id')

    quantity = db.Column(db.Integer, nullable=False)

The relationships will populate correctly as a list of the SimpleThings but I need the quantity field also. Does anyone know how I can get that to show up as attribute through the relationship? SimpleThing.bundeled_things are correct, but for each bundeled_thing I would like to be able to ask for bundled_thing.quantity populated through the relationship rather than having to query the join table.

beaglebets
  • 315
  • 1
  • 12
  • 2
    Possible duplicate of [SQLAlchemy - Self referential Many-to-many relationship with extra column](http://stackoverflow.com/questions/25177451/sqlalchemy-self-referential-many-to-many-relationship-with-extra-column) – univerio Feb 28 '17 at 22:57
  • Now I see what I was doing wrong and have it working based on the answer to that question. Thanks. – beaglebets Mar 01 '17 at 18:01

0 Answers0