Hello, I have a database already created with some tables and records in them, this database was previously created with Laravel.
Now what happens is that it was necessary to modify some relationships between the tables from the Laravel migrations and new tables were added with relationships to the existing ones.
Then I want to know if the migration of the new tables and the updating of the new relationships between tables could be carried out without deleting the records of the tables that are already created.
for instance
mysql> select * from users;
+----+----------------------------+------------------------------+--------------+----------+--------------+------------+--------------------------------------------------------------+---------------------+---------------------+
| id | name | email | telphone | password | profesion_id | company_id | remember_token | created_at | updated_at |
+----+----------------------------+------------------------------+--------------+----------+--------------+------------+--------------------------------------------------------------+---------------------+---------------------+
| 1 | Juan Carlos | [email protected] | 573102184475 | | 2 | 2 | NULL | 2017-05-18 16:22:39 | 2017-05-24 13:47:17 |
| 2 | JUan | [email protected] | NULL | | 1 | 1 | WkYYlmlZq4HVphmo1ZIcKMYd7WlYIliumg2ZqFhENzaJC3ccyJUIkap0KL2S | 2017-05-18 16:23:24 | 2017-05-18 16:23:24 |
+----+----------------------------+------------------------------+--------------+----------+--------------+------------+--------------------------------------------------------------+---------------------+---------------------+
I have the users table that is related to the roles table, the relationship is many to many
+----+------+---------------+---------------------+---------------------+
| id | name | description | created_at | updated_at |
+----+------+---------------+---------------------+---------------------+
| 1 | adm | Administrador | 2017-05-18 16:22:06 | 2017-05-18 16:22:06 |
| 2 | opr | Operador | 2017-05-18 16:22:06 | 2017-05-18 16:22:06 |
| 3 | usr | Usuario | 2017-05-18 16:22:06 | 2017-05-18 16:22:06 |
+----+------+---------------+---------------------+---------------------+
that's why this other table exists
+----+---------+--------+------------+------------+
| id | user_id | rol_id | created_at | updated_at |
+----+---------+--------+------------+------------+
| 1 | 1 | 3 | NULL | NULL |
| 3 | 2 | 1 | NULL | NULL |
| 4 | 2 | 2 | NULL | NULL |
| 5 | 2 | 3 | NULL | NULL |
| 6 | 3 | 1 | NULL | NULL |
| 8 | 5 | 3 | NULL | NULL |
| 9 | 4 | 1 | NULL | NULL |
| 10 | 6 | 3 | NULL | NULL |
| 11 | 7 | 3 | NULL | NULL |
| 12 | 8 | 3 | NULL | NULL |
| 13 | 9 | 3 | NULL | NULL |
| 14 | 10 | 3 | NULL | NULL |
+----+---------+--------+------------+------------+
where the roles are related to the users
then to the migration of the user is this
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->text('name',100);
$table->string('email',191)->unique();
$table->string('telphone', 12)->nullable();
$table->string('password')->nullable();
$table->integer('profesion_id')->unsigned();
$table->foreign('profesion_id')->references('id')->on('profesion_cliente');
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('company');
$table->rememberToken();
$table->timestamps();
});
but then the two changes or updates that will be made are the following:
The migration should look like this
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->text('name',100);
$table->string('email',191)->unique();
$table->string('telphone', 12)->nullable();
$table->string('password')->nullable();
$table->integer('profesion_id')->unsigned();
$table->foreign('profesion_id')->references('id')->on('profesion_cliente');
$table->string('url')->nullable();
$table->integer('rol_id')->unsigned();
$table->foreign('rol_id')->references('id')->on('roles');
$table->rememberToken();
$table->timestamps();
});
A new field was added and a relationship was removed.
So those fields are wanted but without the need to delete all user records.
What you must do is create a new migration, you generate it through the typical command:
php artisan make:migration agregar_campos_usuario
.Next you create the migration schema as usual in its respective file, but instead of using the method
create()
, you usetable()
to add or remove fields: