在Laravel 5.1迁移[SQL SERVER][Linux]时重命名列



我在Laravel 5.1上写一个迁移,创建了一个表后,我重命名表名和列,它运行MySQL数据库的迁移,但在SQL Server 2008上失败,试图重命名列并输出下一个错误:

Next DoctrineDBALDBALException: 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');
  });
}

如果我对重命名列的行进行注释,则迁移会正确运行,因此驱动程序和连接工作正常。

我认为是你重命名主键字段造成的问题。

请测试这个迁移,看看它是否解决了你的:

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'); });

我认为,如果在重命名之前,你放弃主键约束,它应该工作!

最新更新