Self-Referential Association Relationship SQLalchemy

后端 未结 4 1258
南笙
南笙 2020-12-17 03:39

In my flask application with flask-sqlalchemy i need to create association between two contact here is my Contact model

class Contact(db.Model):         


        
相关标签:
4条回答
  • 2020-12-17 03:42

    Self-referential many-to-many relationship with Association Object.

    User Class:

    class User(Base):
        __tablename__ = "User"
        id = Column(String(36), primary_key=True, default=lambda : str(uuid1()))
    

    Association Class:

    class UserIgnore(Base):
        __tablename__ = "UserIgnore"
        id = Column(String(36), primary_key=True, default=lambda : str(uuid1()))
    
        ignored_by_id = Column("ignored_by_id", String(36), ForeignKey("User.id"), primary_key=True)
        ignored_by = relationship("User", backref="ignored_list",  primaryjoin=(User.id == ignored_by_id))
        ignored_id = Column("ignored_id", String(36), ForeignKey("User.id"), primary_key=True)
        ignored = relationship("User", backref="ignored_by_list",  primaryjoin=(User.id == ignored_id))
    

    Access the relationship objects with

    someUser.ignored_list
    

    or

    someUser.ignored_by_list
    

    Thanks to Sean

    0 讨论(0)
  • 2020-12-17 04:01

    it seems that if you change the to_contacts to something like below, your problem will be solved:

    to_contacts = db.relationship('Contact',
                                  secondary='ContactRelation',
                                  primaryjoin='id==contactrelation.from_contact_id',
                                  secondaryjoin='id==contactrelation.to_contact_id',
                                  backref='from_contacts')
    
    0 讨论(0)
  • 2020-12-17 04:08

    Your relationship is not correctly designed. A secondary should be an ordinary table, not a mapped class. If you want the extra data (relation_type) on your ContactRelation, you should use the Association Table pattern described in the SQLAlchemy Relationship docs: http://docs.sqlalchemy.org/en/rel_1_1/orm/basic_relationships.html#association-object

    0 讨论(0)
  • 2020-12-17 04:09

    Thanks to Michel and Simon on SQLAlchemy mailing list i need association_proxy and two relation to Contact relation.

    class Contact(db.Model):
        __tablename__ = 'contact'
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.Unicode(120), nullable=False, unique=False)
        created_on = db.Column(db.DateTime, default=datetime.utcnow)
        birthday = db.Column(db.DateTime)
        background = db.Column(db.Text)
        photo = db.Column(db.Unicode(120))
        user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    
        to_contacts = association_proxy('to_relations', 'to_contact')
        from_contacts = association_proxy('from_relations', 'from_contact')
    
    class ContactRelation(db.Model):
        __tablename__ = 'contactrelation'
        id = db.Column(db.Integer, primary_key=True)
        from_contact_id = db.Column(db.Integer, db.ForeignKey('contact.id'))
        to_contact_id = db.Column(db.Integer, db.ForeignKey('contact.id'))
        relation_type = db.Column(db.String(100), nullable=True)
    
        from_contact = db.relationship(Contact,
                                       primaryjoin=(from_contact_id == Contact.id),
                                       backref='to_relations')
        to_contact = db.relationship(Contact,
                                     primaryjoin=(to_contact_id == Contact.id),
                                     backref='from_relations')
    
    0 讨论(0)
提交回复
热议问题