0

I have two Tables as bellow:

table series:

+-------------+
| Field       |
+-------------+
| series_id   |
| title       |
+-------------+

table season:

+-------------+
| Field       |
+-------------+
| season_id   |
| title       |
| version     |
| duration    |
| series_id   |
+-------------+

In season, series_id as foreign key referees from series(series_id)

Now I want load these two table via sqlalchemy. Is that possible to build a one-to-many relationship between these two tables so that I can access all seasons object of one series?

Wang Nick
  • 385
  • 1
  • 6
  • 17
  • The short answer is yes, it is possible. You can either use [reflection](http://docs.sqlalchemy.org/en/latest/core/reflection.html) and work with core objects (`Table`s etc.), or [automap](http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html) to build ORM models. – Ilja Everilä May 16 '17 at 20:04
  • Possible duplicate of [Can SQLAlchemy automatically create relationships from a database schema?](http://stackoverflow.com/questions/14398329/can-sqlalchemy-automatically-create-relationships-from-a-database-schema) – Ilja Everilä May 16 '17 at 20:06

1 Answers1

1

Yes you can! The series_id field of the season table will be a foreign key to the series table. To get all seasons of a particular series, you will query the 'season' table for all entries which have a particular series_id.

Look at this: http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#querying

zeo
  • 68
  • 10