I have a table like this:
CREATE TABLE import_files (
id SERIAL PRIMARY KEY,
file TEXT NOT NULL,
volume TEXT NOT NULL,
lastimport DATE DEFAULT CURRENT_DATE,
UNIQUE (file,volume)
)
I am trying to import data into the table, such that if a combination of file
and volume does not already exist in the table, a new row should be added,
otherwise if there is an existing row, that row should be updated so that
lastimport is today's date.
However I do not want to do any update if that row is already todays date (the same rows may be presented many times a day).
I am trying to use Postgresql 9.6's new ON CONFLICT clause for this, as follows:
INSERT INTO import_files(file,volume)
VALUES('MyFile1', 'VolA')
ON CONFLICT (file,volume) DO
UPDATE SET lastimport = CURRENT_DATE
WHERE EXCLUDED.lastimport < CURRENT_DATE
RETURNING id
but it does not seem to be working for me. Initial import works fine, but for the update case it does not seem to update even where the WHERE clause is true:
t1=> select * from import_files;
id | file | volume | lastimport
----+---------+--------+------------
1 | MyFile1 | VolA | 2017-05-07
(1 row)
t1=> INSERT INTO import_files(file,volume)
VALUES('MyFile1', 'VolA')
ON CONFLICT (file,volume) DO
UPDATE SET lastimport = CURRENT_DATE
WHERE EXCLUDED.lastimport < CURRENT_DATE
RETURNING id
;
id
----
(0 rows)
INSERT 0 0
Since today is 2017-05-08 I would have expected that row to be updated.
Have I misunderstood the purpose or usage of the WHERE clause in this context?