您好,我已经创建了一个数据库,其中包含一些表和记录,这个数据库之前是用 Laravel 创建的。
现在发生的情况是,有必要修改 Laravel 迁移中的表之间的一些关系,并且添加了与现有表有关系的新表。
然后我想知道是否可以在不删除已经创建的表的记录的情况下进行新表的迁移和表之间新关系的更新。
例如
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 |
+----+----------------------------+------------------------------+--------------+----------+--------------+------------+--------------------------------------------------------------+---------------------+---------------------+
我有与角色表相关的用户表,关系是多对多的
+----+------+---------------+---------------------+---------------------+
| 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 |
+----+------+---------------+---------------------+---------------------+
这就是为什么另一个表存在的原因
+----+---------+--------+------------+------------+
| 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 |
+----+---------+--------+------------+------------+
角色与用户相关的位置
那么到用户的迁移是这样的
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();
});
但是随后将进行的两项更改或更新如下:
迁移应该是这样的
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();
});
添加了一个新字段并删除了一个关系。
所以这些字段是需要的,但不需要删除所有用户记录。
您必须做的是创建一个新的迁移,通过典型的命令生成它:
php artisan make:migration agregar_campos_usuario
.接下来,您像往常一样在其各自的文件中创建迁移模式,但不是使用方法
create()
,而是使用table()
添加或删除字段: