0

I would like to implement a bidirectional friendship relationship using SQLAlchemy.

User A <------> User B <------> User C      User D

Can someone give me an example / code snippet of this?

kungcc
  • 1,832
  • 5
  • 25
  • 48
  • Please look at the code of the question http://stackoverflow.com/q/25177451/99594 and ignore the question itself. The code there is an example of *self-referential many-to-many relationship*. – van Oct 01 '14 at 04:06

1 Answers1

1

You need a many to many JOIN table, because each individual can have many friends. The table will have a composite primary key consisting of two foreign keys that refer back to the individual table.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks duffymo, I understand your point. However, I am not sure how to do it the SQLAlchemy ORM way.. – kungcc Sep 30 '14 at 12:24
  • 1
    ORM means "object relational mapping", so I'm guessing that your Friend class will have a Collection of Friend instances. http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html – duffymo Sep 30 '14 at 12:31