I have 2 tables with a third intermediary table. These tables are (shorted):
https://gist.github.com/9ff8afa793c9150c6b70
Using this the association_proxy correctly reuses existing rows in the database if they already exist. However if I do this:
v = Version.query.first()
v.classifiers = [u"Foo"]
db.session.commit() # A Classifier with trove=u"Foo" is either retrieved or created
v.classifiers = [u"Foo", u"Bar"]
db.session.commit()
An error occurs because SQLAlchemy tried to insert a second row in the intermediary table for this Version + Classifier(u"Foo").
So my question is how can I get it to properly handle the fact that there should only ever be 1 mapping of a particular Version
to a particular Classifier?
Note, I can work around this by doing:
classifiers = [u"Foo", u"Bar"]
for c in classifiers:
if not c in v.classifiers:
v.classifiers.append(c)
But I would really rather have a solution that is contained inside of my Model.