我对一个非常小的迁移有一个奇怪的问题。
基本信息:我目前有3个项目实例:“Dev PC”(离线),“Dev Laptop”(离线)和“QS”(在线)
所以最近两天我写了三次不同的迁移。一个人使用有关模型的6.000个实例的信息导入JSON文件,并将这些模型写入DB。
第二个(现在是失败的)应该向这些模型写一些新数据,第三个将重写一些数据。
如果这是完美的,不应该在这里讨论。那不介意!
所以继续问题:第二次迁移在QS阶段失败,但在两个Dev阶段都有效。
这是第一个错误
-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? (yes/no) [no]:
> yes
[ErrorException]
Creating default object from empty value
然后我试了一下
-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate --force --pretend -vvv
6219
[ErrorException]
Creating default object from empty value
Exception trace:
() at /var/www/vhosts/database/migrations/2016_08_25_080129_import_old_adresses.php:28
Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a
call_user_func() at /var/www/vhosts/vendor/sentry/sentry/lib/Raven/Breadcrumbs/ErrorHandler.php:36
Raven_Breadcrumbs_ErrorHandler->handleError() at /var/www/vhosts/database/migrations/2016_08_25_080129_import_old_adresses.php:28
ImportOldAdresses->up() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:319
Illuminate\Database\Migrations\Migrator->Illuminate\Database\Migrations\{closure}() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Connection.php:656
Illuminate\Database\Connection->pretend() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:320
Illuminate\Database\Migrations\Migrator->getQueries() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:295
Illuminate\Database\Migrations\Migrator->pretendToRun() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:144
Illuminate\Database\Migrations\Migrator->runUp() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:117
Illuminate\Database\Migrations\Migrator->runMigrationList() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:86
Illuminate\Database\Migrations\Migrator->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:77
Illuminate\Database\Console\Migrations\MigrateCommand->fire() at n/a:n/a
call_user_func_array() at /var/www/vhosts/bootstrap/cache/compiled.php:1257
Illuminate\Container\Container->call() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Console/Command.php:169
Illuminate\Console\Command->execute() at /var/www/vhosts/vendor/symfony/console/Command/Command.php:256
Symfony\Component\Console\Command\Command->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Console/Command.php:155
Illuminate\Console\Command->run() at /var/www/vhosts/vendor/symfony/console/Application.php:794
Symfony\Component\Console\Application->doRunCommand() at /var/www/vhosts/vendor/symfony/console/Application.php:186
Symfony\Component\Console\Application->doRun() at /var/www/vhosts/vendor/symfony/console/Application.php:117
Symfony\Component\Console\Application->run() at /var/www/vhosts/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:107
Illuminate\Foundation\Console\Kernel->handle() at /var/www/vhosts/artisan:36
现在我想看看解雇的SQL:
-sh-4.2$ /opt/plesk/php/5.6/bin/php artisan migrate --force --pretend -vvv
array:2 [
0 => array:3 [
"query" => "alter table `orders` add `oldName` varchar(255) not null, add `oldAdress` varchar(255) not null"
"bindings" => []
"time" => 0.0
]
1 => array:3 [
"query" => "select * from `orders` where `orders`.`id` = ? limit 1"
"bindings" => array:1 [
0 => 6219
]
"time" => 0.0
]
]
现在这很令人困惑,因为在数据库上运行查询select * from
订单where
订单.
id = 6219 limit 1
会直接导致1 Entry, Query took 0.0005 seconds.
那么我该怎么做才能调试为什么laravel找不到我的数据库条目? 以下是迁移:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ImportOldAdresses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::table('orders', function (Blueprint $table) {
$table->string('oldName');
$table->string('oldAdress');
});
// Load data to be imported.
$oldOrders = json_decode(File::get('storage/olddata.json'), TRUE);
// Update.
foreach ($oldOrders as $rawOrder) {
$order = \App\Order::find(intval($rawOrder['id'])); // Here's the crash right with the first iteration.
$order->oldName = $rawOrder['oldName'];
$order->oldAdress = $rawOrder['oldAdress'];
$order->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('oldName');
$table->dropColumn('oldAdress');
});
}
}
答案 0 :(得分:0)
好的,我实际上不知道在我进行迁移时第一次发生了什么,但进一步的分析回答了他们的问题。从--pretend
开始只是模拟查询,Order::find($id)
不会返回任何内容。所以发生了错误。
我一遍又一遍地思考并最终再次运行与第一次相同的命令(/opt/plesk/php/5.6/bin/php artisan migrate
)并且它有效。