Associate objects in a many-to-many relationship in SQLAlchemy

后端 未结 1 1489

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

1条回答
  •  眼角桃花
    2020-12-21 17:01

    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

    0 讨论(0)
提交回复
热议问题