I needed to copy on top of fresh Drupal installation only some fields from the old database. In particular I needed to continue on a new site with old nodes and comments. So I have successfully copied all the nodes and comments and got everything displayed correctly on the screen, i.e. number of nodes, comment counts for every nodes, etc. The problem is when I add new comment anywhere on new site it gives this error:

user warning: Duplicate entry '36' for key 1 query: INSERT INTO blog_comments (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (36, 9355, 0, 1, 'dfsd', 'dfsd', 1, '76.126.223.92', 1208609200, 0, 0, 'a:1:{i:0;i:0;}', '0l/', 'admin', '', '') in /home/mysite/public_html/includes/database.mysql.inc on line 172.

Basically, it is counting from anew and in this case trying to add comment number 36, while number of comments from old database is 38,328. So instead of 36th comment, it is supposed to add 38,329th comment.

I made sure to copy everything that I thought is relevant to counts of nodes and comments, namely:

comments
node
node_comment_statistics
node_revisions
users

And I made sure to copy all the tables with Add AUTO_INCREMENT value checked on.

What else I need to copy to make my new Drupal installation to continue to count from total number of nodes and comments of old database.

Comments

yngens’s picture

I have just tried to add a new node and run onto the same problem:

    * user warning: Duplicate entry '129' for key 1 query: INSERT INTO node_revisions (nid, vid, title, body, teaser, timestamp, uid, format, log) VALUES (116, 129, 'asd', 'sd', 'sd', 1208610207, 1, 1, '') in /home/kgus/public_html/includes/database.mysql.inc on line 172.
    * user warning: Duplicate entry '116' for key 1 query: INSERT INTO node_comment_statistics (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (116, 1208610207, NULL, 1, 0) in /home/mysite/public_html/includes/database.mysql.inc on line 172.

Can't understand where Drupal is hiding node and comments counts, all the needed tables were copied with Auto_Increment checked on, as I said above. I wonder what other tables are involved in counting process?

yngens’s picture

I would really appreciate if someone could share an advice on this issue, please.

JohnForsythe’s picture

You need to reset the count in the sequences table. A value called "comments_cid"

--
John Forsythe
Need reliable Drupal hosting?

yngens’s picture

This is a resolution if anyone else runs onto the same issue:


--yngens-- could drupal gurus take a look at: http://drupal.org/node/248592
--Druplicon-- http://drupal.org/node/248592 =-- How to fix comments count in Drupal database? =-- 1 IRC mention
* level09 is looking
--level09-- :D
--modular^-- yngens: there's a table for that
--yngens-- which one?
--modular^-- yngens: sequences
--yngens-- oh thanks, let me try it now
--yngens-- modular, unfortunately it did not help :(
 the same error
--modular^-- did you clear cache?
---- Stmated^ has quit (Read error: 110 (Connection timed out))
--yngens-- nop. let me try
 clearing browser's cache did not help. or should i emty cache table of drupal installation?
--modular^-- what's the value of "comments_cid" in the sequences table?
--yngens-- 38326
--modular^-- and you have that many items in the comments table?
--yngens-- o yes, from the old site
--modular^-- what error message is it giving you now?
--yngens-- user warning: Duplicate entry '42' for key 1 query: INSERT INTO blog_comments (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (42, 9355, 0, 1, 'dsfdsf', 'dsfdsf', 1, '76.126.223.92', 1208634697, 0, 0, 'a:1:{i:0;i:0;}', '0l/', 'admin', '', '') in /home/mysite/public_html/includes/database.mysql.inc on line 172.
--modular^-- hm. blog_comments is a different table from comments
 i don't know if it uses sequences.
 there might be a blog_comments_cid or something like that in the "sequences" table
--yngens-- well, i did not tell the whole story to save space - i am copying the tables from main site to miltisite with blog_
 my multisite works perfectly
 and of course i made needed changes in settings.php
--modular^-- yeah, i think you might need to prefix the name in sequences..
--yngens-- well, i did
--modular^-- or just get rid of the prefix if you don't need it anymore
 but that's probably why it's not working. drupal is getting confused by the prefix or lack of it.
--yngens-- my current prefix settings are:
 $db_prefix = array(
 'default'   =-- 'blog_',
 'users'     =-- '',
 'sessions'  =-- '',
 'persistent_login' =-- '',
 'persistent_login_history' =-- '',
 'role'      =-- '',
 'authmap'   =-- '',
 'profile_fields' =-- '',
 'profile_values' =-- '',
 'users_roles' =-- '',
 'locales_meta' =-- '',
 'locales_source' =-- '',
 'locales_target' =-- '',
 'password_reset' =-- '',
 'password_reset_users' =-- '',
 'bueditor_buttons' =-- '',
 'bueditor_editors' =-- '',
 'buddylist' =-- '',
 'buddylist_buddy_group' =-- '',
 'buddylist_groups' =-- '',
 'buddylist_pending_requests' =-- '',
 'invite' =-- '',
 'guestbook' =-- '',
 'privatemsg' =-- '',
 'privatemsg_archive' =-- '',
 'privatemsg_block_user' =-- '',
 'privatemsg_folder' =-- '',
 'privatemsg_mails' =-- '',
 'subscriptions' =-- '',
 'subscriptions_holding' =-- '',
 );
 all these table are taken from main db
--metals83-- level09:
--modular^-- in the sequences table, do you have an entry called blog_comments_cid?
 or just comments_cid?
--yngens-- all the others, including blog_sequences are generated with blog_
 just comments_cid :) bingo
 u r my hero, now let me try
--yngens-- turns out i have both of them
 \so i guess i just need to delete onw with blog_
 and rename the other
 but then what about node_revisions_vid? i guess i need to rename several relevant tables
--modular^-- i'm not sure exactly, i don't deal with multisite very often
--yngens-- it worked modular!
 finally aftre having troubles with this last 24 hours thank you sooo much!

Mika Pflüger’s picture

Hello,

thanks for this solution - I was really near to give up...

Greetings,

Mika Pflüger