当Laravel在创建表时将id更新为bigInteger的默认ID时,您可能没有注意到现在外键也需要为BIGINT。像我一样,您只是继续使用或留有一些表格:
$table->integer('some_table_id')
在我注意到之前,这给我留下了很多不匹配的表。
如何轻松解决此问题?
首先备份您的数据库
您可以创建迁移并将其添加到up()。它不会做任何事情,但是会告诉您您需要做什么。取消注释回声即可看到需要修复的问题的说明,并确保要更改所有表。将数据库名称更改为您的数据库名称:
$tables = \DB::select('SHOW TABLES');
// this just to get the tabs right in sublime;
$i = 0;
foreach($tables as $table) {
// Tables_in_yourdatabasename
$tableName = $table->Tables_in_mydatabasename;
// echo "#######################\n";
// echo '########## '.$tableName."##########\n\n";
$columns = \DB::select('show columns from ' . $tableName);
foreach ($columns as $value) {
if((substr($value->Field, -2) === 'id') && (substr($value->Type,0,3) === "int" || substr($value->Type,0,6) === "bigint")) {
// echo "$value->Field : type is '$value->Type'\n" ;
if(substr($value->Type,0,6) === "bigint") {
// echo "No Change needed\n";
}
if(substr($value->Type,0,3) === "int") {
// echo "To Update $value->Field to 'BIGINT':\n\n";
if($i === 0) {
echo 'Schema::table(\''.$tableName.'\', function (Blueprint $table) {'."\n";
$i++; //only matters here as we only care about the first row
}else{
echo "\t\t".'Schema::table(\''.$tableName.'\', function (Blueprint $table) {'."\n";
}
echo "\t\t".' $table->bigInteger(\''.$value->Field.'\')->unsigned()->change();'."\n\t\t";
echo '});'."\n";
}
}
}
// echo "\n\n";
}
您可以玩几次,因为您可以将down()空着,只看输出然后再返回。
似乎保留了主键,可为空,默认值等。当您对看到的内容满意时,将其粘贴到真实的迁移文件中。我是否提到了备份您的数据库!这些ID通常不是您想要弄乱的东西。祝你好运!