Many-to-many relations and their entries
You would want to use a unique constraint on the two foreign-keys in your intermediate table.

Then use the `insert into ... on conflict ignore` (or the equivalent) when inserting, to tell the database "if this insert would violate the unique constraint, then discard it". Sqlite, MySQL and Postgres all support this:

* http://docs.peewee-orm.com/en/latest/peewee/api.html#Insert.on_conflict_ignore
* http://docs.peewee-orm.com/en/latest/peewee/querying.html#upsert
Hi,

currently I'm switching an application from PonyORM to Peewee, which is somehow a challenge (both ORMs ;) ). One thing which is difficult to me, is the handling of many-to-many relations. I'd like to avoid the ManyToManyField, so I try to implement it with the suggested default solution.

Selecting from the intermediate table is easy and also the joins, but I'm struggeling with inserting data to it. I usually treat the data to be unique as set, means, that user1 and his relation to tweet1 is unique and can't be inserted a second time. A relation from user1 to tweet2 is ok.

As far as I remember, PonyORM handles this differently and holds combinations as unique (only one user1->tweet1) without any developer intervention. So, how to avoid this with peewee? Or is it necessary to query first the intermediate table if that combination exists or not?

Many thanks in advance! Regards, Thomas