ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – Laravel迁移回滚错误,列数据太长

2019-07-17 09:23:58  阅读:206  来源: 互联网

标签:php mysql laravel laravel-5-3 laravel-5-4


我使用的是Laravel 5.3,迁移对于控制数据库开发来说非常棒.

我的问题是当我将列类型从字符串更改为文本时,一切都运行良好.但是在用户保存长度超过255(varchar)的数据之后.然后我的迁移无法回滚.它会说数据太长了我的专栏.想问大家如何解决这个问题?

================================================== =======

Schema::table('tbname', function(Blueprint $table)
{
    $table->text('value')->change();
});

Schema::table('tbname', function(Blueprint $table)
{
    $table->string('value')->change();
});

================================================== =======

播种机:

$records = [
    [
        'description' => 'The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. '
    ],
    [
        'description' => 'The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. The description is longer than 255. '
    ]
];

foreach ($records as $index => $record) {
    Desc::create($record);
}

解决方法:

您有一个文本字段,其中包含超过255个字符.在回滚中,您尝试将字段更改为varchar(255).此时,MySQL决定:要么截断数据以适应255个字符,要么抛出错误.

如果在严格模式下使用MySQL,MySQL将抛出错误.如果不使用严格模式,MySQL将截断数据而不会出错.

Laravel 5.3默认更改为使用严格模式.

如果您想停止收到此错误,可以通过设置’strict’=>来禁用严格模式config / database.php文件中为false.

请注意,虽然这将抑制错误,但会产生后果.之前导致此错误的数据现在只会被截断而不会出现任何错误.

如果您只想为回滚禁用严格模式,则可以创建第二个禁用严格模式的数据库连接,并告知您的迁移使用该连接进行回滚.但请记住,这将截断此字段中超过255个字符的所有数据.

配置:

'connections' => [
    'mysql' => [
        /* normal connection information */
        'strict' => true,
    ],
    'mysql-rollback' => [
        /* normal connection information */
        'strict' => false,
    ],
],

移民:

public function down()
{
    Schema::connection('mysql-rollback')->table('tbname', function(Blueprint $table) {
        $table->string('value')->change();
    });
}

标签:php,mysql,laravel,laravel-5-3,laravel-5-4
来源: https://codeday.me/bug/20190717/1487215.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有