On Wed, Aug 22, 2018 at 5:41 PM, Alex Rothberg <[email protected]> wrote: > I am using an association model / table to represent a many to many > relationship: > > class Geography(db.Model): > > id = > ... > > class Fund(db.Model): > id = > ... > geography_associations = db.relationship( > lambda: FundGeographyAssociation, > back_populates="fund", > cascade='save-update, merge, delete, delete-orphan' > ) > > geographies = db.relationship( > Geography, > backref="fund", > secondary=lambda: FundGeographyAssociation.__table__, > ) > > class FundGeographyAssociation(db.Model): > fund_id = db.Column( > UUID, db.ForeignKey(Fund.id), primary_key=True, > ) > geography_id = db.Column( > UUID, db.ForeignKey(Geography.id), primary_key=True, > ) > > fund = db.relationship(Fund, back_populates='geography_associations') > > > and then am attempting to update the list of geographies for a Fund using: > fund.geographies = [????] > > > my issue is what to put in ??? when I only have the pk of the geography > model.
it is not a recommended pattern to re-purpose a mapped association class as a "secondary" elsewhere. The ORM does not know that Fund.geography_associations and Fund.geographies refer to the same table and mutations to each of these independently will conflict (see http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#association-object) . The usual pattern is to use an association proxy for Fund.geographies (see http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects). If you want to add a row having only the id of Geography, the most straightforward approach is to append the association object directly: fund.geography_associations = [FundGeoAssoc(geo_id=1)] > > this works: Geography.query.get(id) however this does not: Geography(id=id) > as the latter tries to create a new Geography object leading to conflicts. > The former seems "silly" as it requires an extra query to db to load the > object even though all i need is the geography id to create the association > object. I tried variation of session.merge with load=False however that > doesn't work as the object is transient. > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
