Exploring Model Relationships in Flask

Barbara Streisand
发布: 2024-09-21 14:17:08
原创
340 人浏览过

Exploring Model Relationships in Flask

Coding relationships between classes can be challenging at first! It sounds like a bunch of words thrown together - this thing knows this thing through that thing, but not that other thing. Using real-life examples can be helpful in visualizing those relationships.

For example, say you have some astronauts. Over the course of many years, these astronauts will visit many plants; one planet per mission. So each mission has one astronaut and one planet, and many planets are visited by many astronauts.

In Flask, the relationship between Astronaut and Planet is a many-to-many relationship, while the relationship between Astronaut and Mission and Planet and Mission are both one-to-many. We have three models: The mission model operates as a join table between the astronaut model and the planet model. The classes are called models because they define (or model) the relationships between your data.

So, how do we code these relationships?

I find it easiest to begin with the join table, because I am building out both of the relationships from there.

class Mission(db.Model):
    __tablename__ = 'missions'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
登录后复制

This is what we are starting with for our Mission class.

We know that each mission has one astronaut:

astronaut = db.relationship 
登录后复制

db.relationship defines how two models are related to one another.

Let's connect it to the Astronaut class....

astronaut = db.relationship('Astronaut')
登录后复制

and now let's add the two-way relationship between the two models (Astronaut and Mission):

astronaut = db.relationship('Astronaut', back_populates="missions")
登录后复制

Great work! Since Mission holds both the Astronaut and Planet relationships, let's do the same with planet:

planet = db.relationship('Planet', back_populates="missions")
登录后复制

This is our Mission class with the relationships:

class Mission(db.Model):
    __tablename__ = 'missions'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    astronaut = db.relationship('Astronaut', back_populates="missions")
    planet = db.relationship('Planet', back_populates="missions")

登录后复制

Awesome! Let's go back and take a look at our instructions: The mission model operates as a _join table between the astronaut model and the planet model._
So, we need to link Astronaut to Mission, and Planet to Mission. Let's start with Astronaut:

missions = db.relationship('Mission', back_populates="astronauts")
登录后复制

Missions is plural here, because an astronaut goes on a bunch of them (hopefully!).

And then Planet, which should look similar:

missions = db.relationship('Mission', back_populates="planets")
登录后复制

GREAT! All, together, this looks like:

class Planet(db.Model):
    __tablename__ = 'planets'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    distance_from_earth = db.Column(db.Integer)
    nearest_star = db.Column(db.String)

    missions = db.relationship('Mission', back_populates="planet")


class Astronaut(db.Model):
    __tablename__ = 'astronauts'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    field_of_study = db.Column(db.String)

    missions = db.relationship('Mission', back_populates="astronaut")


class Mission(db.Model):
    __tablename__ = 'missions'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    astronaut = db.relationship('Astronaut', back_populates="astronauts")
    planet = db.relationship('Planet', back_populates="missions")

登录后复制

Lastly, let's add our foreign keys to our missions table. A foreign key is an integer that references an item in another database that links the two together. For example, astronaut 1's foreign key is 1 in the mission table, so every time we see the number 1 in that column, we know it applies to that astronaut!

Mission is the only class that needs foreign keys, since it is in charge of all the relationships.

class Mission(db.Model, SerializerMixin):
    __tablename__ = 'missions'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    astronaut_id = db.Column(db.Integer, db.ForeignKey('astronauts.id'))
    planet_id = db.Column(db.Integer, db.ForeignKey('planets.id'))

    astronaut = db.relationship('Astronaut', back_populates="missions")
    planet = db.relationship('Planet', back_populates="missions")

    serialize_rules = ('-astronaut.missions', '-astronaut.planets')
登录后复制

Fantastic job! We've set up some relationships between our models. Thanks for coding!

Sources: Thank you to Flatiron School for this lab! I changed the class name 'Scientist' to 'Astronaut'.

以上是Exploring Model Relationships in Flask的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!