Duplicate entry 'X' for key 'object_id'
- [x] I've read and understood the contribution guidelines.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened.
I believe this is the same issue as https://github.com/Yoast/wordpress-seo/issues/7566 which was incorrectly marked as resolved.
We receive hundreds of these errors every day:
MysqlError: Duplicate entry 'X' for key 'object_id'
Stack trace:
in mysql_query called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/wp-db.php (1940)
in wpdb::_do_query called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/wp-db.php (1826)
in wpdb::query called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/wp-db.php (2108)
in wpdb::_insert_replace_helper called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/wp-db.php (2029)
in wpdb::insert called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/class-database-proxy.php (47)
in WPSEO_Database_Proxy::insert called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/class-database-proxy.php (87)
in WPSEO_Database_Proxy::upsert called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/class-meta-storage.php (72)
in WPSEO_Meta_Storage::save_meta_data called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/class-meta-storage.php (106)
in WPSEO_Meta_Storage::update_incoming_link_count called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/links/class-link-content-processor.php (115)
in WPSEO_Link_Content_Processor::update_incoming_links called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/links/class-link-content-processor.php (66)
in WPSEO_Link_Content_Processor::update_link_counts called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/links/class-link-content-processor.php (54)
in WPSEO_Link_Content_Processor::process called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/links/class-link-watcher.php (117)
in WPSEO_Link_Watcher::process called at /var/www/releases/20180110170901/wordpress/wp-content/plugins/wordpress-seo/admin/links/class-link-watcher.php (61)
in WPSEO_Link_Watcher::save_post called at ? (?) in call_user_func_array called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/plugin.php (524)
in do_action called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/post.php (3409)
in wp_insert_post called at /var/www/releases/20180110170901/wordpress/wp/wp-includes/post.php (3482)
...
Please describe what you expected to happen and why.
N/A
How can we reproduce this behavior?
- Use an error log
Technical info
- WordPress version: 4.8
- Yoast SEO version: 6.1.1
- Relevant plugins in case of a bug: N/A
I am still seeing this error as well.
Thanks for reporting. We are having a hard time reproducing this issue in our test environment. Can you please let us know what are the exact steps you take from the moment you log into WordPress until the first entry appears in your error log? The more information you can provide the better. Also, were you having these issues with Yoast SEO version 5.9?
Thanks for your help!
@benvaassen Saving a post is one of the steps. What's happening is that you perform a blind INSERT on the database which triggers the error. You need to check first if there is already an existing entry first.
Here's another stack trace when saving a post.
…m/release/wordpress/releases/20180117120121/wordpress/wp/wp-includes/
wp-db.php (1940)
…m/release/wordpress/releases/20180117120121/wordpress/wp/wp-includes/
wp-db.php (1826)
…m/release/wordpress/releases/20180117120121/wordpress/wp/wp-includes/
wp-db.php (2108)
…m/release/wordpress/releases/20180117120121/wordpress/wp/wp-includes/
wp-db.php (2029)
…0121/wordpress/wp-content/plugins/wordpress-seo/admin/
class-database-proxy.php (47)
…0121/wordpress/wp-content/plugins/wordpress-seo/admin/
class-database-proxy.php (87)
…120121/wordpress/wp-content/plugins/wordpress-seo/admin/
class-meta-storage.php (72)
…120121/wordpress/wp-content/plugins/wordpress-seo/admin/
class-meta-storage.php (106)
…/wp-content/plugins/wordpress-seo/admin/links/
class-link-content-processor.php (115)
…/wp-content/plugins/wordpress-seo/admin/links/
class-link-content-processor.php (66)
…/wp-content/plugins/wordpress-seo/admin/links/
class-link-content-processor.php (54)
…/wordpress/wp-content/plugins/wordpress-seo/admin/links/
class-link-watcher.php (117)
…/wordpress/wp-content/plugins/wordpress-seo/admin/links/
class-link-watcher.php (61)
in WPSEO_Link_Watcher::save_post called at ? (?)
...
The code you are referring to is this: https://github.com/Yoast/wordpress-seo/blob/trunk/admin/class-database-proxy.php#L87
Our attempt was to suppress errors on the database whenever we try to do an insert. If that insert fails we do an update.
Seems the better approach is to do a query to check for existence and do an insert or update accordingly.
@moorscode If you really like that approach, you might want to use INSERT IGNORE.
https://chartio.com/resources/tutorials/how-to-insert-if-row-does-not-exist-upsert-in-mysql/
Still not fixed.
@Salamander3 We're aware :) It's currently in our queue candidate, which means it'll be addressed soon.
Same issue here. I have a plugin which inserts post into database. With your plugin enabled, the code below will result with MySQL error "duplicate entry ... for object_id". With your plugin disabled, it works as it's supposed to.
$post = [
// 'post_author' => '1',
'post_content' => (string) $product->description,
'post_excerpt' => (string) $product->short_description,
'post_status' => $set_active ? 'publish' : 'draft',
'post_title' => (string) $product->name,
'post_parent' => '',
'post_type' => 'product',
];
$id = wp_insert_post($post, true)
I am wondering if this issue has been fixed yet?
Nope.
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `internal_link_count`)
VALUES (9, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:65
WPSEO_Link_Content_Processor->store_internal_link_count()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:101
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '9' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `incoming_link_count`)
VALUES (9, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:66
WPSEO_Link_Content_Processor->update_incoming_links()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:115
WPSEO_Meta_Storage->update_incoming_link_count()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:106
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '9' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `internal_link_count`)
VALUES (52889, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:65
WPSEO_Link_Content_Processor->store_internal_link_count()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:101
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '52889' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `incoming_link_count`)
VALUES (52889, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:66
WPSEO_Link_Content_Processor->update_incoming_links()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:115
WPSEO_Meta_Storage->update_incoming_link_count()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:106
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '52889' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `internal_link_count`)
VALUES (54067, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:65
WPSEO_Link_Content_Processor->store_internal_link_count()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:101
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '54067' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `incoming_link_count`)
VALUES (54067, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:66
WPSEO_Link_Content_Processor->update_incoming_links()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:115
WPSEO_Meta_Storage->update_incoming_link_count()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:106
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '54067' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `internal_link_count`)
VALUES (54068, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:65
WPSEO_Link_Content_Processor->store_internal_link_count()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:101
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '54068' for key 'object_id'
INSERT INTO `VGT_yoast_seo_meta` (`object_id`, `incoming_link_count`)
VALUES (54068, '0')
initPages()
wp-content/themes/affiliatetheme/library/stuff.php:1283
wp_update_post()
wp-includes/post.php:3654
wp_insert_post()
wp-includes/post.php:3581
do_action('save_post')
wp-includes/plugin.php:453
WPSEO_Link_Watcher->save_post()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:61
WPSEO_Link_Watcher->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-watcher.php:117
WPSEO_Link_Content_Processor->process()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:54
WPSEO_Link_Content_Processor->update_link_counts()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:66
WPSEO_Link_Content_Processor->update_incoming_links()
wp-content/plugins/wordpress-seo-premium/admin/links/class-link-content-processor.php:115
WPSEO_Meta_Storage->update_incoming_link_count()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:106
WPSEO_Meta_Storage->save_meta_data()
wp-content/plugins/wordpress-seo-premium/admin/class-meta-storage.php:71
WPSEO_Database_Proxy->upsert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:87
WPSEO_Database_Proxy->insert()
wp-content/plugins/wordpress-seo-premium/admin/class-database-proxy.php:47
Plugin: wordpress-seo-premium 1062 Duplicate entry '54068' for key 'object_id'

Please inform the customer of conversation # 333056 when this conversation has been closed.
there is already a solution. I don't think I'm the only one with this problem.
I am also seeing the similar error , is there any fix available?
Everyone does :) I seem to remember someone mentioned that this was going to be a NO FIX, but I am not sure. Yes it is a bit annoying, but afaik it won't do any harm.
This plugin get worse every day. The more you develop it the more bugs it has.
Same problem here on latest version 7.3
Please inform the customer of conversation # 361304 when this conversation has been closed.
Since almost half year is not enough to fix this, below is our temporary workaround /admin/class-meta-storage.php:67 replace function save_meta_data with code below
public function save_meta_data( $meta_id, array $meta_data ) {
$where = array( 'object_id' => $meta_id );
$exists = $this->database_proxy->get_results('SELECT COUNT(*) as `count` FROM `' . $this->database_proxy->get_table_name() .'` WHERE `object_id` = ' . (int)$meta_id );
if($exists[0]->count > 0) {
$saved = $this->database_proxy->update(
array_merge( $where, $meta_data ),
$where
);
} else {
$saved = $this->database_proxy->insert(
array_merge( $where, $meta_data )
);
}
if ( $saved === false ) {
WPSEO_Meta_Table_Accessible::set_inaccessible();
}
}
@rtnet-pl Can you please create a PR for that? Afterwards, we can take a look at your code and merge it if it passes our code review :-)