6

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

class Contact(db.Model):
    __tablename__ = 'contact'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(120), nullable=False, unique=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    to_contacts = db.relationship('Contact',
                                  secondary='ContactRelation',
                                  primaryjoin='id==contactrelation.c.from_contact_id',
                                  secondaryjoin='id==contactrelation.c.to_contact_id',
                                  backref='from_contacts')

and my association class ContactRelation:

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)

i have error :

AttributeError: type object 'ContactRelation' has no attribute 'c'
rezakamalifard
  • 1,289
  • 13
  • 24

4 Answers4

8

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')
rezakamalifard
  • 1,289
  • 13
  • 24
2

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

abhijithvijayan
  • 835
  • 12
  • 17
1

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

Stephen Fuhry
  • 12,624
  • 6
  • 56
  • 55
inklesspen
  • 1,146
  • 6
  • 11
  • ContactRelation class is association object and i want to create association between two contact object. what is problem with this pattern ? – rezakamalifard Sep 22 '14 at 20:27
0

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')
AbdolHosein
  • 528
  • 4
  • 15