How SQLAlchemy `relationship` differs from foreign keys?

Viewed 89

The tutorial gives an example of linking two tables, but it requires users to link the two tables at both the database level (with a foreign key), and the ORM level (with relationship):

user_id = Column(Integer, ForeignKey('users.id'))
user = relationship("User", back_populates="addresses")

I understand foreign keys, as they are needed whether you are using an ORM or not. What I don't understand is why users need to define the relationship at the ORM level. Isn't this repetitive work? Can't SQLAlchemy infer table relationship using foreign keys alone? What functionalities will be missing if there isn't relationship at the ORM level?

0 Answers
Related