Sociological Association SQLalchemy Communication

In my sqlalchemy flask application I need to create a link between two pins here is my model Contact

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 has an error:

AttributeError: type object 'ContactRelation' has no attribute 'c'

      

+3


source to share


3 answers


Thanks to Michelle and Simon on the SQLAlchemy mailing list I need a connection_proxy and two relationship to a contact.



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')

      

+4


source


Your relationship is not well designed. The secondary should be a regular table, not a mapped class. If you want to get additional data (relation_type) in your ContactRelation, you must 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



+1


source


it seems that if you change to_contacts to something like below your problem is solved:

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

      

0


source







All Articles