M
M
Muriam2021-07-25 15:18:25
SQLite
Muriam, 2021-07-25 15:18:25

What is the purpose of the relationship() function in sqlalchemy?

class Family(db.Model):
    __tablename__ = 'family'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    with_plants = db.relationship('Plants', backref='f')


class Genus(db.Model):
    __tablename__ = 'genus'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    with_plants = db.relationship('Plants', backref='g')


class Plants(db.Model):
    __tablename__ = 'plants'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40))
    g_id = db.Column(db.Integer, db.ForeignKey('genus.id'))
    f_id = db.Column(db.Integer, db.ForeignKey('family.id'))


As I understand it, relationship connects tables, but not their fields (as does a foreign key), but objects? How to understand it?
I have the genus table connected to the plants table through a relationship (and through a ForeignKey too). And also the family table with the plants table.

How to link tables: plants c genus, and genus c family ?

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