Reputation: 11746
My current DB table looks like this:
CREATE TABLE IF NOT EXISTS `image_ratings` (
`rating_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`photo_id` int(11) unsigned NOT NULL DEFAULT '0',
`rating` tinyint(3) unsigned NOT NULL DEFAULT '0',
`rated_user_id` int(11) unsigned NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`rating_id`),
KEY `user_id` (`user_id`),
KEY `rated_user_id` (`rated_user_id`),
KEY `photo_id` (`photo_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
When I make a call like so:
INSERT INTO image_ratings (user_id, photo_id, rating, rated_user_id) VALUES (5, 5975, 8, 5973 ) ON DUPLICATE KEY UPDATE user_id=5, photo_id=5975, rating=8, rated_user_id=5973, timestamp=NOW()
It creates a new entry even though I already have any entry for the same user_id, rated_user_id, and photo_id. Is this because I have a PRIMARY key defined for the rating_id?
I would expect it to update the existing entry with the new rating value.
Any thoughts?
Upvotes: 0
Views: 83
Reputation: 211560
You need a UNIQUE
index on the compound key that defines the unique tuple you're looking for. In your case, it seems like user_id
and photo_id
would be relevant. Without this the only unique key is the rating_id
which will be populated automatically for you every time.
Also, please don't use MyISAM on important production databases. It's very brittle compared to InnoDB and will end up corrupted beyond repair sooner or later.
Upvotes: 1