|
|
|
|
|
by Arch-TK
26 days ago
|
|
Okay, so I have an object like: User {
name
friends: List<Friend>
posts: List<Post>
}
Let's say we have a "MappedUser" which is derived from this type by this ORM.I now do: user = get_mapped_user()
for post in user.friends[0].friends[0].posts {
...
}
Ignoring "get_mapped_user()" how does our user object work?What happens when I access `.friends`? Does it give me an empty list, because I didn't ask for it? I am not aware of anything that calls itself an ORM which merely does: user: User = map_from_relational_to_user(query_user())
Not only is it difficult to conceptualise how this operation would ever meaningfully work for any non-trivial query, it's also difficult to see how it would even work for trivial queries.ORMs, at their core, try to abstract away something like `user.friends[0].friends[0].posts` more or less into some underlying queries against a relational database. The main distinction between them being in the availability and first-class nature of the escape hatches when this operation inevitably becomes slow. |
|
That depends on the rest of your code. If you are using something like the active record or data mapper pattern then it would reach out and fetch more results. If you don't have such mechanics in place then an empty list is possible. We don't have enough information here to say what happens.
> I am not aware of anything that calls itself an ORM which merely does
When your code merely does that, what do you call it?
> ORMs, at their core, try to abstract away something like `user.friends[0].friends[0].posts` more or less into some underlying queries against a relational database.
Active record/data mapper tries to abstract that. ORMs are a necessary piece of active record/data mapper, but one part of a larger system. You also need things like a query builder. ORM alone is not sufficient for these patterns.
> into some underlying queries against a relational database.
Unlikely. SQL is mentioned in the headline for a reason. Nobody uses relational databases in the real world. The only remaining relational database engines that are still maintained really only exist for educational purposes. I understand why you might think a relational database is necessary given that ORM stands for Object Relational Mapping, but as ORM operates on data, not databases, the data can be relational even if the backing database isn't. It simply becomes another mapping step to see them become compatible.