使用 php artisan migrate 时,在迁移中更改 table 名称,使用 tinker 保存对象时出错
when using php artisan migrate, change table name in migration, error occur when using tinker to save object
这是我的迁移,我正在使用 mysql,当我 运行 迁移时,它成功了,我查看了我的 mysql 数据库, table我想要的名字是"receivedDocument"
class CreateReceivedDocumentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('received_documents', function (Blueprint $table) {
$table->id();
$table->dateTime('receivedDate')->nullable();
$table->string('subjectDescription')->nullable();
$table->string('fromEntity')->nullable();
$table->string('documentNumber')->nullable();
$table->integer('documentTypeId')->nullable();
$table->dateTime('documentDate')->nullable();
$table->integer('responseTypeId')->nullable();
$table->text('remarks')->nullable();
$table->timestamps();
});
Schema::rename('received_documents', 'receivedDocument');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::rename('receivedDocument', 'received_documents');
Schema::dropIfExists('received_documents');
}
}
当我使用 tinker 创建和保存记录时,它失败了。 Tinker 似乎不知道我在迁移中更改了 table 名称。
>>> $rd = new \App\ReceivedDocument();
=> App\ReceivedDocument {#3036}
>>> $rd->subjectDescription = 'description sample';
=> "description sample"
>>> $rd->documentNumber = '1234/2020';
=> "1234/2020"
>>> $rd->save();
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or
view not found: 1146 Table 'dossier.received_documents' doesn't exist (SQL: inse
rt into `received_documents` (`subjectDescription`, `documentNumber`, `updated_a
t`, `created_at`) values (description sample, 1234/2020, 2020-06-11 10:18:00, 20
20-06-11 10:18:00))'
如何告诉 Tinker 我在迁移中有什么变化?
在您的模型中,定义 table 如下:
protected $table = 'receivedDocument';
这是我的迁移,我正在使用 mysql,当我 运行 迁移时,它成功了,我查看了我的 mysql 数据库, table我想要的名字是"receivedDocument"
class CreateReceivedDocumentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('received_documents', function (Blueprint $table) {
$table->id();
$table->dateTime('receivedDate')->nullable();
$table->string('subjectDescription')->nullable();
$table->string('fromEntity')->nullable();
$table->string('documentNumber')->nullable();
$table->integer('documentTypeId')->nullable();
$table->dateTime('documentDate')->nullable();
$table->integer('responseTypeId')->nullable();
$table->text('remarks')->nullable();
$table->timestamps();
});
Schema::rename('received_documents', 'receivedDocument');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::rename('receivedDocument', 'received_documents');
Schema::dropIfExists('received_documents');
}
}
当我使用 tinker 创建和保存记录时,它失败了。 Tinker 似乎不知道我在迁移中更改了 table 名称。
>>> $rd = new \App\ReceivedDocument();
=> App\ReceivedDocument {#3036}
>>> $rd->subjectDescription = 'description sample';
=> "description sample"
>>> $rd->documentNumber = '1234/2020';
=> "1234/2020"
>>> $rd->save();
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or
view not found: 1146 Table 'dossier.received_documents' doesn't exist (SQL: inse
rt into `received_documents` (`subjectDescription`, `documentNumber`, `updated_a
t`, `created_at`) values (description sample, 1234/2020, 2020-06-11 10:18:00, 20
20-06-11 10:18:00))'
如何告诉 Tinker 我在迁移中有什么变化?
在您的模型中,定义 table 如下:
protected $table = 'receivedDocument';