A
A
Alexander Ivanov2020-01-28 21:52:57
JavaScript
Alexander Ivanov, 2020-01-28 21:52:57

Why did they come up with models in django/laravel if there are sql queries?

After all, in order to use them, you need to re-learn a whole separate technology.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
alex, 2019-05-03
@kiril9011

A
Artem Lisovsky, 2020-01-29
@alexivanov77

ORM is the highest level of abstraction when working with a database.
It allows using simpler structures to make queries to the database. At what to any DB from supported.
An example of "simpler" constructs (django):

Child.objects.filter(name__in=['Паша', 'Илья'], age__gte=18, parent__name='Игорь')

in SQL it would be:
SELECT * FROM "main_child" INNER JOIN "main_parent" ON ("main_child"."parent_id" = "main_parent"."id") WHERE ("main_child"."age" >= 18 AND "main_child"."name" IN (Паша, Илья) AND "main_parent"."name" = Игорь)

Cumbersome, isn't it? Not all quotation marks have been placed here yet.
An example of a django project is here: https://repl.it/repls/SneakyStickyHypercard
In addition to beautiful queries in the ORM, database tables are beautifully painted, and there are usually built-in migrations, injection protection, working with FK/M2M/indexes, and more.
Look how beautifully the DB structure is described (django):
from django.db import models

class Parent(models.Model):
  name = models.CharField(max_length=50)

class Child(models.Model):
  name = models.CharField(max_length=150)
  age = models.IntegerField(default=0)
  parent = models.ForeignKey(Parent, on_delete=models.CASCADE)

In general, not using an ORM today is stupid (just like writing a project in bare php/python/..., use a framework). ORM simplifies life and speeds up development. ORM does not always form optimal queries, but that's what we programmers are to control and decide. With an ORM, you can always send a "raw" SQL query, so I don't see a problem with that.

S
Shohruh Shaimardonov, 2020-01-28
@joeberetta

What you call models is actually called ORM/ODM.
And they were invented to reduce the cost of a separate database developer (sql developer) and give ordinary backenders a chance to write code without thinking about database queries. Well, the second side (also follows from the first) for greater flexibility / portability of the code. Those. if at some point in the development of the project you decided for some reason to change the database (which naturally has a different syntax), you did not have to rewrite everything again. Orm/odm will do everything for you.

A
Alexander Aksentiev, 2020-01-28
@Sanasol

And how do models "replace" SQL queries? This is what the SQL builder does, not the models.

After all, in order to use them, you need to re-learn a whole separate technology.

Why did they come up with php if there is C ++.
And other important questions about the futility of being.

V
Vladimir Kuts, 2020-01-28
@fox_12

re-learn a whole separate technology

What new standalone technology?
Models are the essence of objects. Accordingly, it is convenient to work with them using object-oriented programming technologies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question