DEV Community

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

Collapse
 
tinazhouhui profile image
Tina

Hey Philippe, thanks for a great question :)

If you are asking about the "visual" aspect of it and hence the maintainability, I admit, it can get pretty messy :) Though some ORM tools provide solution for that as well, like php's Doctrine 2 has this QueryBuilder that makes the the complex queries clearer.

now if you are asking about the execution of complex queries, then I would say, never blindly trust the ORM and always check what it is doing under the hood. Is it sending one query or sending 100 from my one line? Luckily, most of the ORMs do allow you to adjust your queries so it does what you want, but it does require some time spent with the documentation.

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.