Laravel Migrations

0 目的

创建Migrations主要的功能是为了让团队能保住使用的数据库结构一致

1 开玩

  1. 相关的命令(php artisan list)

     migrate:install     Create the migration repository
     migrate:refresh     Reset and re-run all migrations
     migrate:reset       Rollback all database migrations
     migrate:rollback    Rollback the last database migration
     migrate:status      Show the status of each migration
    

    创建数据库表(手动创建表不会作用.),因为输入命令后,会在verdor/composer/autoload_classmap.php里映射类.如下

         'Create_Disease_Type' => $baseDir . '/database/migrations/2015_11_02_153746_Create_Disease_Type.php',
         'CreateInstitutes' => $baseDir . '/database/migrations/2015_11_02_143144_Create_Institutes.php',
    
     php artisan make:migration Create_Users --create=Users  
    
  2. 创建基础类,设置id、时间戳和软删除.

    一开始自己创建了个Base_Table,不过那样也会创建一个表,所以直接把方法写在Migration

     /**
     * 增加基本属性
     * @param Blueprint $table
     */
     public function add_base_attribute(Blueprint $table)
     {
         $table->increments('id');
         //创建created_at和updated_at
         $table->timestamps();
         //创建deleted_at
         $table->softDeletes();
         return $table;
     }
    
  3. 创建外键,注意要先定义外键属性,再设定关系.

     public function up()
     {
         Schema::create('Institutes', function (Blueprint $table) {
             $table->unsignedInteger('institute_ID');
             $table->foreign('institute_ID')
             ->references('id')->on('institutes')
             ->onDelete('cascade');
             $table = parent::add_base_attribute($table);
         });
     }  
    
  4. 修改表

     php artisan make:migration Add_Name_To_Samples_Table --table=Samples
    
     注意,要是新增列,直接和创建表一样语法
    
     但要修改已由列需要调用change()方法,例如:
    
          $table->string('name', 50)->change();
    

    可能报不是dba的错误

     [RuntimeException]
     Changing columns for table "Samples" requires Doctrine DBAL; install "doctrine/dbal".
    

    在composer.json中配置

     "require": {
         ...
         "doctrine/dbal": "v2.4.2"
     },  
    

错误处理

  1. 有依赖关系的表,要把外键的表先创建.
  2. 外键的属性类型要一致,否则会报错General error: 1215 Cannot add foreign key constraint

     //主表
     $table->increments('id');
    
     //从表
     $table->unsignedInteger('institute_ID');
         $table->foreign('institute_ID')
         ->references('id')->on('institutes')
         ->onDelete('cascade');      the
    
  3. migrations类名不过有下划线

    报错

     PHP Fatal error:  Class 'CreateInstitutes' not found in /Users/maizhikun/Learning/apache_sites/HapLab_Laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 324
    
     [Symfony\Component\Debug\Exception\FatalErrorException]
     Class 'CreateInstitutes' not found
    
     //看了下报错地方的源代码.估计是下划线导致.   
     public function resolve($file)
     {
         $file = implode('_', array_slice(explode('_', $file), 4));
         $class = Str::studly($file);
         return new $class;
     }
    

    后来又发现了错误.最后确定了是文件名(假如有下划线)会去掉下划线(从时间之后的下划线)然后找相对应的类名.

  4. php artisan migrate:refresh,reset,rollback都无法执行

    提示报错

     [Symfony\Component\Debug\Exception\FatalErrorException]
      Class 'Illuminate\Database\Migrations\Schema' not found 
    

    解决办法:

    Illuminate\Support\Facades\Schema复制到Illuminate\Database\Schema

    并且

    修改命名空间和use

     namespace Illuminate\Database\Migrations;
    
     use Illuminate\Support\Facades\Facade;
    

    参考

php

Comments