Run prettier everywhere
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
||||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
||||
|
||||
# Table of Contents
|
||||
|
||||
- [SQLAlchemy Anti-Patterns](#sqlalchemy-anti-patterns)
|
||||
@@ -12,14 +13,12 @@
|
||||
|
||||
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
||||
|
||||
SQLAlchemy Anti-Patterns
|
||||
========================
|
||||
# SQLAlchemy Anti-Patterns
|
||||
|
||||
This is a list of what I consider [SQLAlchemy](http://www.sqlalchemy.org/)
|
||||
anti-patterns.
|
||||
|
||||
Abusing lazily loaded relationships
|
||||
-----------------------------------
|
||||
## Abusing lazily loaded relationships
|
||||
|
||||
Bad:
|
||||
|
||||
@@ -34,10 +33,10 @@ class Customer(Base):
|
||||
|
||||
This suffers from severe performance inefficiencies:
|
||||
|
||||
* The toaster will be loaded, as well as its toast. This involves creating and
|
||||
- The toaster will be loaded, as well as its toast. This involves creating and
|
||||
issuing the SQL query, waiting for the database to return, and instantiating
|
||||
all those objects.
|
||||
* `has_valid_toast` does not actually care about those objects. It just returns
|
||||
- `has_valid_toast` does not actually care about those objects. It just returns
|
||||
a boolean.
|
||||
|
||||
A better way would be to issue a SQL `EXISTS` query so that the database
|
||||
@@ -61,8 +60,7 @@ class Customer(Base):
|
||||
This query might not always be the fastest if those relationships are small,
|
||||
and eagerly loaded.
|
||||
|
||||
Explicit session passing
|
||||
------------------------
|
||||
## Explicit session passing
|
||||
|
||||
TODO
|
||||
|
||||
@@ -73,13 +71,11 @@ def toaster_exists(toaster_id, session):
|
||||
...
|
||||
```
|
||||
|
||||
Implicit transaction handling
|
||||
-----------------------------
|
||||
## Implicit transaction handling
|
||||
|
||||
TODO
|
||||
|
||||
Loading the full object when checking for object existence
|
||||
----------------------------------------------------------
|
||||
## Loading the full object when checking for object existence
|
||||
|
||||
Bad:
|
||||
|
||||
@@ -90,8 +86,8 @@ def toaster_exists(toaster_id):
|
||||
|
||||
This is inefficient because it:
|
||||
|
||||
* Queries all the columns from the database (including any eagerly loaded joins)
|
||||
* Instantiates and maps all data on the Toaster model
|
||||
- Queries all the columns from the database (including any eagerly loaded joins)
|
||||
- Instantiates and maps all data on the Toaster model
|
||||
|
||||
The database query would look something like this. You can see that all columns
|
||||
are selected to be loaded by the ORM.
|
||||
@@ -123,8 +119,7 @@ FROM toasters
|
||||
WHERE toasters.id = 1) AS anon_1
|
||||
```
|
||||
|
||||
Using identity as comparator
|
||||
----------------------------
|
||||
## Using identity as comparator
|
||||
|
||||
Bad:
|
||||
|
||||
@@ -169,8 +164,8 @@ toasters = session.query(Toaster).filter(Toaster.deleted_at.is_(None)).all()
|
||||
```
|
||||
|
||||
See docs for
|
||||
[is_](http://docs.sqlalchemy.org/en/rel_1_0/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.is_).
|
||||
[is\_](http://docs.sqlalchemy.org/en/rel_1_0/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.is_).
|
||||
|
||||
## Returning `None` instead of raising a `NoResultFound` exception
|
||||
|
||||
See [Returning nothing instead of raising NotFound exception](https://github.com/charlax/antipatterns/blob/master/code-antipatterns.md#returning-nothing-instead-of-raising-notfound-exception).
|
||||
See [Returning nothing instead of raising NotFound exception](https://github.com/charlax/antipatterns/blob/master/code-antipatterns.md#returning-nothing-instead-of-raising-notfound-exception).
|
||||
|
||||
Reference in New Issue
Block a user