Skip to content

Commit f440aa0

Browse files
authored
Revert "Remove note re: ON CONFLICT ON CONSTRAINT (github#1522)" (github#1532)
This reverts commit 7e49315.
1 parent 7e49315 commit f440aa0

File tree

1 file changed

+21
-10
lines changed
  • timescaledb/how-to-guides/write-data

1 file changed

+21
-10
lines changed

timescaledb/how-to-guides/write-data/upsert.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ keywords: [upsert, hypertables]
55
---
66

77
# Upsert data
8-
98
Upserting is shorthand for:
10-
119
* Inserting a new row if a matching row doesn't already exist
1210
* Either updating the existing row, or doing nothing, if a matching row
1311
already exists
@@ -22,11 +20,9 @@ If you have a primary key, you automatically have a unique index.
2220
</highlight>
2321

2422
## Create a table with a unique constraint
25-
2623
The examples in this section use a `conditions` table with a unique constraint
2724
on the columns `(time, location)`. To create a unique constraint, use `UNIQUE
2825
(<COLUMNS>)` while defining your table:
29-
3026
```sql
3127
CREATE TABLE conditions (
3228
time TIMESTAMPTZ NOT NULL,
@@ -40,7 +36,6 @@ CREATE TABLE conditions (
4036
You can also create a unique constraint after the table is created. Use the
4137
syntax `ALTER TABLE ... ADD CONSTRAINT ... UNIQUE`. In this example, the
4238
constraint is named `conditions_time_location`:
43-
4439
```sql
4540
ALTER TABLE conditions
4641
ADD CONSTRAINT conditions_time_location
@@ -61,14 +56,12 @@ well. For more information, see the section on
6156
</highlight>
6257

6358
## Insert or update data to a table with a unique constraint
64-
6559
You can tell the database to insert new data if it doesn't violate the
6660
constraint, and to update the existing row if it does. Use the syntax `INSERT
6761
INTO ... VALUES ... ON CONFLICT ... DO UPDATE`.
6862

6963
For example, to update the `temperature` and `humidity` values if a row with the
70-
specified `time` and `location` already exists, run:
71-
64+
specified `time` and `location` already exists, run:
7265
```sql
7366
INSERT INTO conditions
7467
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.2, 50.1)
@@ -78,19 +71,37 @@ INSERT INTO conditions
7871
```
7972

8073
## Insert or do nothing to a table with a unique constraint
81-
8274
You can also tell the database to do nothing if the constraint is violated. The
8375
new data is not inserted, and the old row is not updated. This is useful when
8476
writing many rows as one batch, to prevent the entire transaction from failing.
8577
The database engine skips the row and moves on.
8678

8779
To insert or do nothing, use the syntax `INSERT INTO ... VALUES ... ON CONFLICT
8880
DO NOTHING`:
89-
9081
```sql
9182
INSERT INTO conditions
9283
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.1, 50.0)
9384
ON CONFLICT DO NOTHING;
9485
```
9586

87+
<highlight type="note">
88+
You cannot specify a constraint by name when using `INSERT ... ON CONFLICT` on
89+
hypertables. Work around this by explicitly specifying the constrained columns.
90+
For example, instead of this:
91+
92+
```sql
93+
-- This does not work
94+
INSERT INTO conditions
95+
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.1, 50.0)
96+
ON CONFLICT ON CONSTRAINT conditions_time_location DO NOTHING;
97+
```
98+
99+
Do this:
100+
```sql
101+
INSERT INTO conditions
102+
VALUES ('2017-07-28 11:42:42.846621+00', 'office', 70.1, 50.0)
103+
ON CONFLICT (time, location) DO NOTHING;
104+
```
105+
</highlight>
106+
96107
[postgres-upsert]: https://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT

0 commit comments

Comments
 (0)