Skip to content

Instantly share code, notes, and snippets.

@a-suenami
Created July 1, 2014 05:48
Show Gist options
  • Save a-suenami/32665e688d7b16e576fe to your computer and use it in GitHub Desktop.
Save a-suenami/32665e688d7b16e576fe to your computer and use it in GitHub Desktop.
PostgreSQL の timestamp with time zone の等価判定について ref: http://qiita.com/a-suenami/items/dea19256f6ec672d67f1
postgres=# CREATE TABLE orders ( product_id integer, ordered_at timestamp with time zone, CONSTRAINT pkey PRIMARY KEY (product_id, ordered_at) ) ;
CREATE TABLE
postgres=# \d orders
Table "public.orders"
Column | Type | Modifiers
------------+--------------------------+-----------
product_id | integer | not null
ordered_at | timestamp with time zone | not null
Indexes:
"pkey" PRIMARY KEY, btree (product_id, ordered_at)
postgres=# INSERT INTO orders (product_id, ordered_at) VALUES (1, '2014-07-01 14:00:00') ;
INSERT 0 1
postgres=# SELECT product_id, ordered_at FROM orders ;
product_id | ordered_at
------------+------------------------
1 | 2014-07-01 14:00:00+09
(1 row)
postgres=# INSERT INTO orders (product_id, ordered_at) VALUES (1, '2014-07-01 14:00:00 UTC') ;
INSERT 0 1
postgres=# SELECT product_id, ordered_at FROM orders ;
product_id | ordered_at
------------+------------------------
1 | 2014-07-01 14:00:00+09
1 | 2014-07-01 23:00:00+09
(2 rows)
postgres=# INSERT INTO orders (product_id, ordered_at) VALUES (1, '2014-07-01 05:00:00 UTC') ;
ERROR: duplicate key value violates unique constraint "pkey"
DETAIL: Key (product_id, ordered_at)=(1, 2014-07-01 14:00:00+09) already exists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment