1

I'm trying to achieve (what I can only now call the infamous) many-to-many or association pattern in SQLAlchemy, but to the same table. My requirement is to be able to define a Relationship as linking two Roles.

Regarding only the columns in the ORM, below is what I'm trying to achieve:

class Relationship(Base):
    __tablename__ = 'relationship'
    id = Column(Integer, autoincrement=True, primary_key=True)
    subject_id = Column(ForeignKey('role.id'), primary_key=True)
    object_id = Column(ForeignKey('role.id'), primary_key=True)
    relationship_name = Column(String)

class Role(Base):
    __tablename__ = 'role'
    id = Column(Integer, autoincrement=True, primary_key=True)

I've tried adapting the pattern at SQLAlchemy Many-to-Many, but of course I run into the multiple join paths problem. I would prefer not to use the association pattern, because I want to augment data to the Relationship and so have it available as a class.

How can I do this in the declarative style?

richarddb
  • 49
  • 1
  • 4
  • Regarding the declarative preference, [this is a solution](https://stackoverflow.com/questions/1889251/sqlalchemy-many-to-many-relationship-on-a-single-table) that does not declare a class. – richarddb Aug 29 '20 at 18:23

0 Answers0