You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features.md
+40-5Lines changed: 40 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,7 @@
1
-
# Overview
2
-
3
1
# HStoreField
4
2
`psqlextra.fields.HStoreField` is based on Django's [HStoreField](https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/fields/#hstorefield) and therefor supports everything Django does natively, plus more.
5
3
6
-
## Constraints
7
-
### uniqueness
4
+
## uniqueness
8
5
The `uniqueness` constraint can be added on one or more `hstore` keys, similar to how a `UNIQUE` constraint can be added to a column. Setting this option causes unique indexes to be created on the specified keys.
9
6
10
7
You can specify a `list` of strings to specify the keys that must be marked as unique:
@@ -26,7 +23,7 @@ Uniqueness can also be enforced "together", similar to Django's [unique_together
26
23
27
24
In the example above, `key1` and `key2` must unique **together**, and `key3` must unique on its own. By default, none of the keys are marked as "unique".
28
25
29
-
###required
26
+
## required
30
27
The `required` option can be added to ensure that the specified `hstore` keys are set for every row. This is similar to a `NOT NULL` constraint on a column. You can specify a list of `hstore` keys that are required:
31
28
32
29
from psqlextra.fields import HStoreField
@@ -39,3 +36,41 @@ The `required` option can be added to ensure that the specified `hstore` keys ar
Both calls to `create` would fail in the example above since they do not provide a non-null value for `key1`. By default, none of the keys are required.
39
+
40
+
# Upsert
41
+
An "upsert" is an operation where a piece of data is inserted/created if it doesn't exist yet and updated (overwritten) when it already exists. Django has long provided this functionality through [`update_or_create`](https://docs.djangoproject.com/en/1.10/ref/models/querysets/#update-or-create). It does this by first checking whether the record exists and creating it not.
42
+
43
+
The major problem with this approach is possibility of race conditions. In between the `SELECT` and `INSERT`, another process could perform the `INSERT`. The last `INSERT` would most likely fail because it would be duplicating a `UNIQUE` constaint.
44
+
45
+
In order to combat this, PostgreSQL added native upserts. Also known as [`ON CONFLICT DO ...`](https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT). This allows a user to specify what to do when a conflict occurs.
46
+
47
+
48
+
## upsert
49
+
The `upsert` method attempts to insert a row with the specified data or updates (and overwrites) the duplicate row, and then returns the primary key of the row that was created/updated:
Note that a single call to `upsert` results in a single `INSERT INTO` statement. It is therefor much faster and much safer than anything Django currently has to offer.
63
+
64
+
## upsert_and_get
65
+
`upsert_and_get` does the same thing as `upsert`, but returns a model instance rather than the primary key of the row that was created/updated. This also happens in a single query using `RETURNING` clause on the `INSERT INTO` statement:
Copy file name to clipboardExpand all lines: docs/index.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,15 +5,15 @@ django-postgres-extra aims to make all of PostgreSQL's awesome features availabl
5
5
With seamless we mean that any features we add will work truly seamlessly. You should not have to manually modify your migrations to work with fields and objects provided by this package.
6
6
7
7
## PostgreSQL features
8
-
We are currently aiming on having the following features available:
8
+
Explore the [Features](/features/) page for detailed instructions and information on how to use all features.
@@ -44,5 +44,6 @@ In order to use this package, your project must be using:
44
44
45
45
* Python 3.5, or newer
46
46
* PostgreSQL 9.6 or newer
47
+
* Django 1.10 or newer
47
48
48
49
Python 3.5 is required because type hints are used. A feature only available in Python 3.5 and newer. PostgreSQL 9.6 is required to take advantage of the latest features such as `ltree`.
0 commit comments