Skip to content

Commit 2274448

Browse files
committed
working on explaining the downsides of ORMs
1 parent d0f79d6 commit 2274448

4 files changed

Lines changed: 89 additions & 28 deletions

File tree

‎all.html‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,10 +3281,23 @@ <h1>Object-relational mappers (ORMs)</h1>
32813281
<h2>Why are ORMs useful?</h2>
32823282
<p>ORMs provide a high-level abstraction upon a
32833283
<a href="/databases.html">relational database</a> that allows a developer to write
3284-
Python code instead of SQL to create, read, update and delete data in
3285-
their database. Developers can use the programming language they are
3286-
comfortable with to work with a database instead of writing SQL statements or
3287-
stored procedures.</p>
3284+
Python code instead of SQL to create, read, update and delete data and
3285+
schemas in their database. Developers can use the programming language they
3286+
are comfortable with to work with a database instead of writing SQL
3287+
statements or stored procedures. </p>
3288+
<p>For example, in a developer would write the following SQL statement to
3289+
retrieve every row in the USERS table where the <code>zip_code</code> column is 94107:</p>
3290+
<div class="highlight"><pre>SELECT * FROM USERS WHERE zip_code=94107;
3291+
</pre></div>
3292+
3293+
3294+
<p>The equivalent Django ORM query would instead look like the following Python
3295+
code:</p>
3296+
<div class="highlight"><pre># obtain everyone in the 94107 zip code and assign to users variable
3297+
users = Users.objects.filter(zip_code=94107)
3298+
</pre></div>
3299+
3300+
32883301
<p>The ability to write Python code instead of SQL can speed up web application
32893302
development, especially at the beginning of a project. The potential
32903303
development speed boost comes from not having to switch from Python code
@@ -3313,21 +3326,27 @@ <h2>Do I have to use an ORM for my web application?</h2>
33133326
library called a <em>database connector</em>, such as
33143327
<a href="http://initd.org/psycopg/">psycopg</a> (for PostgreSQL)
33153328
or <a href="https://pypi.python.org/pypi/MySQL-python/1.2.5">MySQL-python</a> (for
3316-
MySQL). Take a look at the table below which shows that ORMs can work with
3317-
different web frameworks and database connectors.</p>
3329+
MySQL). Take a look at the table below which shows how ORMs can work with
3330+
different web frameworks and connectors and relational databases.</p>
33183331
<p><img src="theme/img/orm-examples.png" width="100%" alt="Examples of how varying Python ORMs can work with different connectors and backends." class="technical-diagram" /></p>
3319-
<p>For example, in the above table SQLAlchemy can work with or without an
3320-
accompanying web framework as well as different database connectors.
3332+
<p>The above table shows for example that SQLAlchemy can work with or without
3333+
an accompanying web framework as well as varying database connectors.
33213334
Developers can also use ORMs without a web framework, such as when creating
33223335
a data analysis tool or a batch script without a user interface. </p>
33233336
<h2>What are the downsides of using an ORM?</h2>
33243337
<p>There are numerous downsides of ORMs, including</p>
33253338
<ol>
33263339
<li>impedance mismatch</li>
3327-
<li>difficulty writing complex queries</li>
3340+
<li>difficulty expressing complex queries</li>
33283341
<li>potential for reduced performance</li>
33293342
<li>shifting complexity into the application from the database layer</li>
33303343
</ol>
3344+
<h3>Impedance mismatch</h3>
3345+
<p>The phrase "impedance mismatch" is commonly used in conjunction with ORMs.
3346+
Impedance mismatch is a catch-all term for the difficulties that occur when
3347+
moving data between relational tables and application objects. The gist
3348+
is that the way a developer uses objects is different from how data is
3349+
stored and joined in relational tables.</p>
33313350
<h2>Django's ORM</h2>
33323351
<p>The <a href="/django.html">Django</a> web framework comes with
33333352
its own built-in object-relational mapping module, generally referred to

‎feeds/all.atom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt Makai</title><link href="http://www.fullstackpython.com/" rel="alternate"></link><link href="http://www.fullstackpython.com/feeds/all.atom.xml" rel="self"></link><id>http://www.fullstackpython.com/</id><updated>2015-06-14T17:20:54Z</updated></feed>
2+
<feed xmlns="http://www.w3.org/2005/Atom"><title>Matt Makai</title><link href="http://www.fullstackpython.com/" rel="alternate"></link><link href="http://www.fullstackpython.com/feeds/all.atom.xml" rel="self"></link><id>http://www.fullstackpython.com/</id><updated>2015-06-14T18:40:47Z</updated></feed>

‎object-relational-mappers-orms.html‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,23 @@ <h1>Object-relational mappers (ORMs)</h1>
4141
<h2>Why are ORMs useful?</h2>
4242
<p>ORMs provide a high-level abstraction upon a
4343
<a href="/databases.html">relational database</a> that allows a developer to write
44-
Python code instead of SQL to create, read, update and delete data in
45-
their database. Developers can use the programming language they are
46-
comfortable with to work with a database instead of writing SQL statements or
47-
stored procedures.</p>
44+
Python code instead of SQL to create, read, update and delete data and
45+
schemas in their database. Developers can use the programming language they
46+
are comfortable with to work with a database instead of writing SQL
47+
statements or stored procedures. </p>
48+
<p>For example, in a developer would write the following SQL statement to
49+
retrieve every row in the USERS table where the <code>zip_code</code> column is 94107:</p>
50+
<div class="highlight"><pre>SELECT * FROM USERS WHERE zip_code=94107;
51+
</pre></div>
52+
53+
54+
<p>The equivalent Django ORM query would instead look like the following Python
55+
code:</p>
56+
<div class="highlight"><pre># obtain everyone in the 94107 zip code and assign to users variable
57+
users = Users.objects.filter(zip_code=94107)
58+
</pre></div>
59+
60+
4861
<p>The ability to write Python code instead of SQL can speed up web application
4962
development, especially at the beginning of a project. The potential
5063
development speed boost comes from not having to switch from Python code
@@ -73,21 +86,27 @@ <h2>Do I have to use an ORM for my web application?</h2>
7386
library called a <em>database connector</em>, such as
7487
<a href="http://initd.org/psycopg/">psycopg</a> (for PostgreSQL)
7588
or <a href="https://pypi.python.org/pypi/MySQL-python/1.2.5">MySQL-python</a> (for
76-
MySQL). Take a look at the table below which shows that ORMs can work with
77-
different web frameworks and database connectors.</p>
89+
MySQL). Take a look at the table below which shows how ORMs can work with
90+
different web frameworks and connectors and relational databases.</p>
7891
<p><img src="theme/img/orm-examples.png" width="100%" alt="Examples of how varying Python ORMs can work with different connectors and backends." class="technical-diagram" /></p>
79-
<p>For example, in the above table SQLAlchemy can work with or without an
80-
accompanying web framework as well as different database connectors.
92+
<p>The above table shows for example that SQLAlchemy can work with or without
93+
an accompanying web framework as well as varying database connectors.
8194
Developers can also use ORMs without a web framework, such as when creating
8295
a data analysis tool or a batch script without a user interface. </p>
8396
<h2>What are the downsides of using an ORM?</h2>
8497
<p>There are numerous downsides of ORMs, including</p>
8598
<ol>
8699
<li>impedance mismatch</li>
87-
<li>difficulty writing complex queries</li>
100+
<li>difficulty expressing complex queries</li>
88101
<li>potential for reduced performance</li>
89102
<li>shifting complexity into the application from the database layer</li>
90103
</ol>
104+
<h3>Impedance mismatch</h3>
105+
<p>The phrase "impedance mismatch" is commonly used in conjunction with ORMs.
106+
Impedance mismatch is a catch-all term for the difficulties that occur when
107+
moving data between relational tables and application objects. The gist
108+
is that the way a developer uses objects is different from how data is
109+
stored and joined in relational tables.</p>
91110
<h2>Django's ORM</h2>
92111
<p>The <a href="/django.html">Django</a> web framework comes with
93112
its own built-in object-relational mapping module, generally referred to

‎source/content/pages/05-data/04-object-relational-mappers.markdown‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ are more commonly used in application code.
1414
## Why are ORMs useful?
1515
ORMs provide a high-level abstraction upon a
1616
[relational database](/databases.html) that allows a developer to write
17-
Python code instead of SQL to create, read, update and delete data in
18-
their database. Developers can use the programming language they are
19-
comfortable with to work with a database instead of writing SQL statements or
20-
stored procedures.
17+
Python code instead of SQL to create, read, update and delete data and
18+
schemas in their database. Developers can use the programming language they
19+
are comfortable with to work with a database instead of writing SQL
20+
statements or stored procedures.
21+
22+
For example, in a developer would write the following SQL statement to
23+
retrieve every row in the USERS table where the ``zip_code`` column is 94107:
24+
25+
SELECT * FROM USERS WHERE zip_code=94107;
26+
27+
28+
The equivalent Django ORM query would instead look like the following Python
29+
code:
30+
31+
# obtain everyone in the 94107 zip code and assign to users variable
32+
users = Users.objects.filter(zip_code=94107)
33+
2134

2235
The ability to write Python code instead of SQL can speed up web application
2336
development, especially at the beginning of a project. The potential
@@ -51,13 +64,13 @@ databases. In fact, the low-level access is typically provided by another
5164
library called a *database connector*, such as
5265
[psycopg](http://initd.org/psycopg/) (for PostgreSQL)
5366
or [MySQL-python](https://pypi.python.org/pypi/MySQL-python/1.2.5) (for
54-
MySQL). Take a look at the table below which shows that ORMs can work with
55-
different web frameworks and database connectors.
67+
MySQL). Take a look at the table below which shows how ORMs can work with
68+
different web frameworks and connectors and relational databases.
5669

5770
<img src="theme/img/orm-examples.png" width="100%" alt="Examples of how varying Python ORMs can work with different connectors and backends." class="technical-diagram" />
5871

59-
For example, in the above table SQLAlchemy can work with or without an
60-
accompanying web framework as well as different database connectors.
72+
The above table shows for example that SQLAlchemy can work with or without
73+
an accompanying web framework as well as varying database connectors.
6174
Developers can also use ORMs without a web framework, such as when creating
6275
a data analysis tool or a batch script without a user interface.
6376

@@ -66,11 +79,20 @@ a data analysis tool or a batch script without a user interface.
6679
There are numerous downsides of ORMs, including
6780

6881
1. impedance mismatch
69-
1. difficulty writing complex queries
82+
1. difficulty expressing complex queries
7083
1. potential for reduced performance
7184
1. shifting complexity into the application from the database layer
7285

7386

87+
### Impedance mismatch
88+
The phrase "impedance mismatch" is commonly used in conjunction with ORMs.
89+
Impedance mismatch is a catch-all term for the difficulties that occur when
90+
moving data between relational tables and application objects. The gist
91+
is that the way a developer uses objects is different from how data is
92+
stored and joined in relational tables.
93+
94+
95+
7496
## Django's ORM
7597
The [Django](/django.html) web framework comes with
7698
its own built-in object-relational mapping module, generally referred to
@@ -121,6 +143,7 @@ is required for commercial projects. The license is a one-time payment
121143
and does not necessitate a recurring fee.
122144

123145

146+
124147
## Schema migrations
125148
Schema migrations, for example when you need to add a new column to an
126149
existing table in your database, are not technically part of ORMs. However,

0 commit comments

Comments
 (0)