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.
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.