gpt4 book ai didi

linux - 重命名 Laravel 5.1 迁移中的列 [SQL SERVER][Linux]

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:50 26 4
gpt4 key购买 nike

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

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

最佳答案

我认为导致问题的原因是您重命名了主键字段。

请测试此迁移以查看它是否解决了您的问题:

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

我想如果在重命名之前,你放弃主键约束,它应该会成功!

关于linux - 重命名 Laravel 5.1 迁移中的列 [SQL SERVER][Linux],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com