A
A
Alex M.2015-05-01 02:10:40
MySQL
Alex M., 2015-05-01 02:10:40

How to use 'ON DUPLICATE KEY UPDATE' correctly?

Hi all. I ran into a problem:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '43117-series' for key 'titles_tmdb_id_type_unique' SQL:
INSERT INTO titles 
    (title,language,country,updated_at,tmdb_id,type) 
VALUES 
    (Teen Choice Awards, En, US, 2015-05-01 01:44:14, 43117,series) 
ON DUPLICATE KEY UPDATE 
title = values(title), 
language = values(language), 
country = values(country), 
updated_at = values(updated_at), 
tmdb_id = values(tmdb_id),
type = values(type)

Help find solutions!
The table structure is like this:
CREATE TABLE IF NOT EXISTS `titles` (
  `id` bigint(20) unsigned NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `type` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'movie',
....
  `tmdb_id` bigint(20) unsigned DEFAULT NULL,
....
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `language` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `titles`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `titles_tmdb_id_type_unique` (`tmdb_id`,`type`);

ALTER TABLE `titles`
  MODIFY `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@mgyk, 2015-05-01
_

take away

tmdb_id = values(tmdb_id),
type = values(type)

from the request, they already matched

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question