Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to restore the users to the new database from the old database

This is what I try at first,

INSERT INTO db_after_deleted.users SELECT * FROM db_before_deleted.users where company_id = 30

this throws error:

#1062 - Duplicate entry '128' for key 'users.PRIMARY'

I thought may be its not allowing to use the same id, so I tried with this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT INTO db_after_deleted.users SELECT name,email,email_verified_at,password,remember_token, role_id,company_id,company_role, email_notification, email_subscription,first_time, created_at, updated_at, deleted_at FROM db_before_deleted.users where company_id = 30

This throws:

#1136 - Column count doesn't match value count at row 1

I checked and confirmed that both does have the same number of column though. How could I restore users from company_id 30. What could be the best and safest way.

>Solution :

From the second error, the inserted table and source table column didn’t correspond.

I would use explicitly specify the inserted columns name after INSERT INTO

INSERT INTO db_after_deleted.users
            (NAME,
             email,
             email_verified_at,
             password,
             remember_token,
             role_id,
             company_id,
             company_role,
             email_notification,
             email_subscription,
             first_time,
             created_at,
             updated_at,
             deleted_at)
SELECT NAME,
       email,
       email_verified_at,
       password,
       remember_token,
       role_id,
       company_id,
       company_role,
       email_notification,
       email_subscription,
       first_time,
       created_at,
       updated_at,
       deleted_at
FROM   db_before_deleted.users
WHERE  company_id = 30 

The first error caused there are some data that duplicate exists in db_after_deleted.users, you can try to use this query to get those data then do delete them before do insert data.

SELECT *
FROM   db_before_deleted.users b
INNER JOIN db_after_deleted.users a
ON a.id = b.id
WHERE b.company_id = 30 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading