1
votes

I am working on python with postgres database ,i have two tables which have many to many relation and third table in which relations are saved , i need to apply flask-msearch on relations table in a way that it fetches & search in both tables and give the resultant output , I have implemented on single table it is working fine there but not working in many to many relation

Here is my model which is working correctly

class JobpostJobpost(db.Model):
    __tablename__ = 'jobpost_jobpost'
    __searchable__ = ['company_name']  
    id = db.Column('job_id', Integer, primary_key=True)
    company_name = db.Column(String(255))

& to search i have to use this query

JobpostJobpost.query.msearch(query,fields=['company_name'])

it gives accurate result but when i use it for multi table it gives error here i my code of model for multitable

class TblDegreeSpecilizationRelation(db.Model):
    __tablename__ = 'tbl_degree_specilization_relation'
    __searchable__ = ['tbl_suggestion_degrees.name','tbl_suggestion_degrees.tags'] # ,'tbl_suggestion_degree_specilization.name' ,'tbl_suggestion_degree_specilization.tags'

    id = db.Column('id', Integer, nullable=False, primary_key=True)
    degree_id = db.Column('degree_id', ForeignKey(u'tbl_suggestion_degrees.id', ondelete=u'CASCADE'))
    specilization_id  = db.Column('specilization_id', ForeignKey(u'tbl_suggestion_degree_specilization.id', ondelete=u'CASCADE'))
1
I have solved this , i was adding table name alongwith .columnname in Searchable , i just added degree = relationship(u'TblSuggestionDegrees',backref=db.backref( 'post', uselist=False), uselist=False) in and then i added degree.name & degree.tags in searchable & it is working correctly with correct output.Muhammad Aadil Banaras

1 Answers

1
votes

I have sloved this issue , i was using tablename alongwith .column name i changed it as , Model

class TblDegreeSpecilizationRelation(db.Model):
__tablename__ = 'tbl_degree_specilization_relation'
__searchable__ = ['degree.name','degree.tags', 'specilization.name', 'specilization.tags'] # ,'tbl_suggestion_degree_specilization.name' ,'tbl_suggestion_degree_specilization.tags'

id = db.Column('id', Integer, nullable=False, primary_key=True)
degree_id = db.Column('degree_id', ForeignKey(u'tbl_suggestion_degrees.id', ondelete=u'CASCADE'))
specilization_id  = db.Column('specilization_id', ForeignKey(u'tbl_suggestion_degree_specilization.id', ondelete=u'CASCADE'))

degree = relationship(u'TblSuggestionDegrees',backref=db.backref(
        'post', uselist=False), uselist=False)
specilization = relationship(u'TblSuggestionDegreeSpecilization',backref=db.backref(
        'post', uselist=False), uselist=False)

Now it is working fine