Connecting Multiple Tables
Learn how to connect multiple tables in a database
Issues of having multiple tables
Suppose we had a second table that paired up the author with their birth year.
Press + to interact
class BirthYear(base):__tablename__ = 'birth_years'author = Column(types.String(length=50), primary_key=True)birth_year = Column(types.Integer, nullable=False)## Add the new table to the databasebase.metadata.create_all(db)session.add_all([BirthYear(author='Lewis Carroll', birth_year=1832),BirthYear(author='Kurt Vonnegut', birth_year=1922),BirthYear(author='Annie Dillard', birth_year=1945)])
Now suppose we need to select all available titles from authors born in the 20th century. One way to do this would be to find all such authors and look them up in the other Book
table one by one. Or, we ...
Access this course and 1400+ top-rated courses and projects.