Forums

sqla;chemy query with two tables

Hi,

I have two models applicants and jobs

I have a url /jobs where I want to display all the jobs but exclude those jobs where applicant has already applied.

applicant_jobs_list   = Applicants.query.filter_by(applicant_id=current_user.id).all()
jobs_list             = JobsTB.query.filter_by().all()
  1. How do I exclude job_ids in JobsTB that match jobIid's in Applicants?
  2. How should I pass these result to my view...?

what do your database models look like, and what web framework are you using?

I am using Flask, SqlAlchemy

 class Applicants(db.Model):
        __tablename__ = 'applicants'
        id          = db.Column(db.Integer(), primary_key=True)
        user_id_id    = db.Column(db.Integer(), db.ForeignKey('user.id'))
        company_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
        job_id      = db.Column(db.Integer(), db.ForeignKey('new_jobs.id'))

        def __init__(self, user_id=None, company_id =None, job_id=None):
            self.user_id       = user_id
            self.company_id = company_id 
            self.job_id         = job_id

        def __repr__(self):
            return '{self.job_id}'.format(self=self)



class JobsTB(db.Model):
    __tablename__ = 'jobs'
    id              = db.Column(db.Integer(), primary_key=True)
    job_description = db.Column(db.String(255))
    pay_per_hour    = db.Column(db.Integer())
    start_PickADay  = db.Column(db.String(25))
    end_PickADay    = db.Column(db.String(25))
    start_time      = db.Column(db.String(25))
    end_time        = db.Column(db.String(25))
    agree_tos       = db.Column(db.Boolean(),   default=False)
    job_active      = db.Column(db.Boolean(),   default=True)
    posted_at       = db.Column(db.String(25),  default= datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    updated_ago     = db.Column(db.String(25))
    company_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
    company_name   = db.Column(db.String(35))


    def __init__(self, job_description  =   None,
                    pay_per_hour        =   None,
                    start_PickADay      =   None,
                    end_PickADay        =   None,
                    start_time          =   None,
                    end_time            =   None,
                    agree_tos           =   None,
                    job_active          =   None,
                    company_id       =   None,
                    updated_ago         =   None,
                    company_name       =   None):
        self.job_description= job_description
        self.pay_per_hour   = pay_per_hour
        self.start_PickADay = start_PickADay
        self.end_PickADay   = end_PickADay
        self.start_time     = start_time
        self.end_time       = end_time
        self.agree_tos      = agree_tos
        self.job_active     = job_active
        self.company_id  = company_id 
        self.updated_ago    = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.company_name  = company_name

    def __repr__(self):
        return '<JobsTB {self.company_id }>'.format(self=self)




class User(db.Model, UserMixin):
    id          = db.Column(db.Integer,     primary_key=True)
    email       = db.Column(db.String(255), unique=True)
    password    = db.Column(db.String(255))
    active      = db.Column(db.Boolean())
    confirmed_at= db.Column(db.DateTime(),  default=datetime.now())
    op          = db.Column(db.String(2))
    roles       = db.relationship('Role',   secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    def __init__(self, email=None, password=None, active=None, op=None, roles=[]):
        self.email      = email
        self.password   = password
        self.active     = active
        self.roles      = roles
        self.op         = op

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return '<User {self.email}>'.format(self=self)



class ApplicantProfileTB(db.Model):
    __tablename__ = 'applicant_profiles'
    id                  = db.Column(db.Integer(),   primary_key=True)
    first_name          = db.Column(db.String(50))
    last_name           = db.Column(db.String(50))
    first_line_address  = db.Column(db.String(255))
    second_line_address = db.Column(db.String(255), nullable=True)
    postcode            = db.Column(db.String(255))
    phone               = db.Column(db.String(20))
    agree_tos           = db.Column(db.Boolean(),   default=False)
    applicant_id            = db.Column(db.Integer(),   db.ForeignKey('user.id'))
    created_at          = db.Column(db.String(50))


class CompanyProfileTB(db.Model):
    __tablename__ = 'company_profiles'
    id                  = db.Column(db.Integer(), primary_key=True)
    company_name       = db.Column(db.String(50))
    company_industry    = db.Column(db.String(50))
    first_line_address  = db.Column(db.String(50))
    second_line_address = db.Column(db.String(50))
    postcode            = db.Column(db.String(50))
    phone               = db.Column(db.Integer())
    agree_tos           = db.Column(db.Boolean(), default=False)
    company_id         = db.Column(db.Integer(), db.ForeignKey('user.id'))
    created_at          = db.Column(db.String(50))

See this

Thanks. I am going through this. But at the first glance, is there any major difference in using Base vs db.Model ?

I think you should use db.Model if you are using flask. See here.