
Object-relational impedance mismatch - amirouche
https://en.wikipedia.org/wiki/Object-relational_impedance_mismatch
======
amirouche
I am under the impression that ORM are not helpful. I am in particular not
sure why people still use them. I think that part of the response lies in the
fact that people don't want to learn SQL, at least that was my position before
reading about the subject. Now that I know SQL and knowing that correctly
using an ORM can not be done without understanding the generated SQL, I am
wondering why ORMs are very popular.

ORM seems to me like a dangerous indirection.

Nowdays I use aiohttp (a python asyncio http framework) with psycopg2 (via
aiopg) on a rather large application with only a serialization/validation
framework named Colander. I don't feel the need for an ORM. It does the job.
What I am doing wrong?

~~~
PaulHoule
I felt that ORM systems were negative 15 years ago. They have improved quite a
bit since then. An increasing number of competitive frameworks have come out
and the incumbent frameworks have improved. The situation is much better than
when that "Vietnam" article came out. I think many people use a lightweight
ORM such as Colander and if you are happy with that, that is great.

These days though I am using SQL very differently than the old days. I used to
use MySQL, PostgreSQL and sometimes Microsoft SQL Server but lately if I use
SQL it is SQLite. Today I am much likely to use some kind of graph or document
database such as DynamoDB or OpenLink Virtuoso, but it has been a long process
to get confident enough in the new tools.

What I see missing from most db tools is the ability to map functions from the
host langage to the query language in back. For instance, if you write
something like

lambda x: 5 _x-2_ x*x+7

you should be able to compile that the SQL or SPARQL or whatever equivalent.
Microsoft's LINQ framework approaches that with a lot of specific features
built into the runtime and language. In either Python or Java you can run a
(or the) compiler/decompiler on an expression, make an AST, convert that AST
into a query AST and send it to the database engine.

~~~
tome
Opaleye can do this

[https://hackage.haskell.org/package/opaleye](https://hackage.haskell.org/package/opaleye)

(The generated code may not be the easiest to read, but I hope the point comes
across)

    
    
        > let paulHoulesFunction = (\x -> 5 * x - 2 * x * x + 7); paulHoulesFunction :: Num a
        => a -> a
        
        > traverse_ putStrLn (showSqlForPostgres (queryTable table1))
        SELECT "column10_1" as "result1_2",
               "column21_1" as "result2_2"
        FROM (SELECT *
              FROM (SELECT "column1" as "column10_1",
                           "column2" as "column21_1"
                    FROM "table1" as "T1") as "T1") as "T1"
        
        > traverse_ putStrLn (showSqlForPostgres ((fmap . fmap) paulHoulesFunction (queryTable table1)))
        SELECT "column10_1" as "result1_2",
               ((5 * ("column21_1")) - ((2 * ("column21_1")) * ("column21_1"))) + 7 as "result2_2"
        FROM (SELECT *
              FROM (SELECT "column1" as "column10_1",
                           "column2" as "column21_1"
                    FROM "table1" as "T1") as "T1") as "T1"

