The situation:
So, I have a basic many-to-many relationship in SQLAlchemy using an association table.
For example, a person can attend many parties, and a party can have many persons as guests:
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
name = db.Column(db.String(50))
class SexyParty(Base):
__tablename__ = 'sexy_party'
id = Column(Integer, primary_key=True)
guests = relationship('Person', secondary='guest_association',
lazy='dynamic', backref='parties')
guest_association = Table(
'guest_association',
Column('user_id', Integer(), ForeignKey('person.id')),
Column('sexyparty.id', Integer(), ForeignKey('sexyparty.id'))
)
Normally if I wanted to add a list of guests to a party, I would do something like this:
my_guests = [prince, olivia, brittany, me]
my_party.guests = guests
db.session.commit()
...where prince, olivia and brittany are all <Person>
instances, and my_party is a <SexyParty>
instance.
My question:
I'd like to add guests to a party using person ID's rather than instances. For example:
guest_ids = [1, 2, 3, 5]
my_party.guests = guest_ids # <-- This fails, because guest_ids
# are not <Person> instances
I could always load the instances from the databases, but that would entail an unnecessary DB query just to set a simple many-to-many relationships.
How would I go about setting the .guests
attribute using a list of person_id's?
There has to be a simple way to do this since the association table ultimately represents the many-to-many relationship using ID's anyway...
thanks in advance, hope the question is clear.