B
B
B1ackGh0st2019-09-10 05:01:56
Flask
B1ackGh0st, 2019-09-10 05:01:56

How to display records from three linked tables in the form of a list?

Hey!
There are three tables in PostgreSQL. branches, departments, sections. How to form a request to get such a format at the output?
branch 1
department1 area1.1 area1.2
department2 area2.1
area2.2
branch 2 ..... .... ... Using

SQLAlchemy

branches_workshop = db.Table('branches_workshop',
                     db.Column('branches_id', db.Integer(), db.ForeignKey('branches.id')),
                     db.Column('workshop_id', db.Integer(), db.ForeignKey('workshop.id'))
                     )


workshop_department = db.Table('workshop_department',
                     db.Column('workshop_id', db.Integer(), db.ForeignKey('workshop.id')),
                     db.Column('department_id', db.Integer(), db.ForeignKey('department.id'))
                     )

class Branches(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))       
    description = db.Column(db.Text)        
    phone_code = db.Column(db.String(255))  

class Workshop(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    description = db.Column(db.Text)
    branches = db.relationship('Branches', secondary=branches_workshop)

class Department(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    description = db.Column(db.Text)
    workshop = db.relationship('Workshop', secondary=workshop_department)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question