我在Laravel 5.1上编写了一个迁移,在创建了一个表后,我重命名了表名和列,它运行了MySQL数据库的迁移,但在SQL Server 2008上尝试重命名列和输出失败了下一个错误:
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 :(得分: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');
});
我认为如果在重命名之前删除主键约束,它应该可以解决!