2016-09-23 47 views
0

我在Laravel 5.1上寫了一個遷移,創建了一個表之後,我重命名了表名和列,它可以運行MySQL數據庫的遷移,但是SQL Server 2008中失敗嘗試重新命名列,並輸出一個錯誤:重命名Laravel 5.1遷移列[SQL SERVER] [Linux]

Next Doctrine\DBAL\DBALException: An exception occurred while executing 'SELECT col.name, 
        type.name AS type, 
        col.max_length AS length, 
        ~col.is_nullable AS notnull, 
        def.definition AS [default], 
        col.scale, 
        col.precision, 
        col.is_identity AS autoincrement, 
        col.collation_name AS collation, 
        CAST(prop.value AS NVARCHAR(MAX)) AS comment -- CAST avoids driver error for sql_variant type 
      FROM  sys.columns AS col 
      JOIN  sys.types AS type 
      ON  col.user_type_id = type.user_type_id 
      JOIN  sys.objects AS obj 
      ON  col.object_id = obj.object_id 
      JOIN  sys.schemas AS scm 
      ON  obj.schema_id = scm.schema_id 
      LEFT JOIN sys.default_constraints def 
      ON  col.default_object_id = def.object_id 
      AND  col.object_id = def.parent_object_id 
      LEFT JOIN sys.extended_properties AS prop 
      ON  obj.object_id = prop.major_id 
      AND  col.column_id = prop.minor_id 
      AND  prop.name = 'MS_Description' 
      WHERE  obj.type = 'U' 
      AND  (obj.name = 'roles' AND scm.name = SCHEMA_NAME())': 

我需要在這兩個數據庫的工作遷移。我的遷移代碼是:

public function up() 
{ 
    Schema::create('cat_tipo_usuario', function (Blueprint $table) { 
     $table->increments('id_tipo_usuario'); 
     $table->string('txt_tipo_usuario'); 
     $table->timestamps(); 
    }); 
    //Se renombra la tabla 
    Schema::rename('cat_tipo_usuario','roles'); 
    //Se cambia el nombre de las columnas 
    Schema::table('roles',function($tabla){ 
    $tabla->renameColumn('id_tipo_usuario','id'); 
    $tabla->renameColumn('txt_tipo_usuario','nombre'); 
    }); 
} 

如果我評論我重命名列的行,遷移將正確運行,因此驅動程序和連接運行良好。

回答

0

我認爲是什麼導致問題是你重命名你的主鍵字段。

請測試這種遷移,看它是否解決了您:

Schema::table('roles',function($tabla){ $table->dropPrimary('id_tipo_usuario'); $tabla->renameColumn('id_tipo_usuario','id'); $table->primary('id'); $tabla->renameColumn('txt_tipo_usuario','nombre'); });

我想,如果重命名之前,您刪除主鍵約束,它應該工作了!