I've created a platform about three years ago when I had less knowledge about how to create efficient databases. I have now re-written the platform into Laravel but I come across a problem.
In the old database I made use of VARCHAR as a primary key in my table Customer. In other tables this primary key was used as foreign key. In my new migration I have created an AI as a primary key, and another column to store the username.
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username');
$table->timestamps();
});
I need to import the data from the other table (where the varchar was used as a foreignkey) into the Laravel database. However I need to replace this old foreign key with the new primary key. What is an easy way of doing this? It's a large table with atleast 3 million+ rows .
I made a small script to open the old table in a CSV file. Inside a loop I go trough every line and search for the Customer (where username = old foreign key) by the old foreign key and then replaces it with the new primary key of the Customer table. But this is very time consuming.
I would use an extra column in the customers table where I store the old primary key and the same for the other tables where you use it as foreing key.
Then I can run something like
INSERT INTO table2
SELECT * FROM table1
And then do an update of the foreign key Something like
UPDATE table set foreign key = (select id from table2, table where table.old_id = table2.old_id
Haven't thought about these ideas before. I am definitly going to try them out tomorrow and see how it works. The approach I am currently using is working but very time consuming, like 24+ hours.