Skip to content Skip to sidebar Skip to footer

Associate Objects In A Many-to-many Relationship In Sqlalchemy

I'm trying to associate two database objects via a bidirectional many-to-many relationship using SQLAlchemy. I need the association to exist in a junction table. The tables and dat

Solution 1:

the association proxy requires either that target object has a single-argument constructor which will create the appropriate intermediary object, or that creator is specified which establishes how to create a ColorVehicle:

vehicles = association_proxy('vehicles_and_colours', 'vehicles', 
                creator=lambda vehicle: ColorVehicle(vehicle=vehicle))

colours = association_proxy('vehicles_and_colours', 'colours', 
               creator=lambda color: ColorVehicle(color=color))

this is fully documented at:

https://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#creation-of-new-values

Post a Comment for "Associate Objects In A Many-to-many Relationship In Sqlalchemy"