DEV Community

Discussion on: Introduction to Object-relational mapping: the what, why, when and how of ORM

Collapse
 
philippejbruno profile image
Philippe Bruno

Hi Tina, thanks for your quick reply. From your article, my understanding is that each row from a table gets translated into an instance of a class where the various columns are represented as properties. With a simple table, I can totally picture this in my mind. But what about a case where you have a many-to-many relationship like students can be enrolled in multiple classes and a class has many enrolled students... What would be the resulting mapping? If each student is an instance of a Student class, how would you map the fact the student can be enrolled in one or more classes? Do you have a property of the student class that would be some sort of array of classes? I am lost. Maybe this question could be an opportunity for you to write a part two to an otherwise excellent article.

Thread Thread
 
tinazhouhui profile image
Tina

Hey Philippe, sorry for not such a quick reply this time.

The beauty of ORM is that the properties do not have to be columns but can be an instance of a different table. So, in your example, a student would be of class Student and would have property class, which would be an instance of a Class. and through that property, you would be able to access student.class.name or whichever other properties the Class class has. The ORM uses various relationship properties to ensure that these relationships are behaving correctly.. Here, I am sending you a link to the SQL Alchemy documentation that shows the different relationships, including many-to-many.