Laravel 8 SQLSTATE[42S02]:未找到基础 table 或视图:1146 Table 即使我正在尝试创建此 table
Laravel 8 SQLSTATE[42S02]: Base table or view not found: 1146 Table even though I am trying to create this table
我正在使用 Laravel 8。我正在尝试创建一个新的 table,而这个 table 在数据库中不存在,并且没有任何其他具有相同名称的 table。我做了迁移,这是迁移代码:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCertificateTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('certificate', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('vac_id');
$table->time('last_shot_date');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('vac_id')->references('id')->on('vaccination');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('certificate', function (Blueprint $table) {
//
});
}
}
做完后
php artisan migrate
这是我收到的完整错误消息:
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vaccinationappointme
nts.certificate' doesn't exist (SQL: alter table `certificate` add `id` bigint un
signed not null auto_increment primary key, add `user_id` bigint unsigned not nul
l, add `vac_id` bigint unsigned not null, add `last_shot_date` time not null, add
`created_at` timestamp null, add `updated_at` timestamp null)
at D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illumi
nate\Database\Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll
format the error
689▕ // message to include the bindings with SQL, which will make thi
s exception a
690▕ // lot more helpful to the developer instead of just the databas
e's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕
• A table was not found: You might have forgotten to run your migrations. You c
an run your migrations using `php artisan migrate`.
https://laravel.com/docs/master/migrations#running-migrations
1 D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table '
vaccinationappointments.certificate' doesn't exist")
2 D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
PDOStatement::execute()
我试过:
php artisan migrate:refresh
还是一样的问题。我没用过模型。
如果要创建带有迁移的 table,您需要使用 ::create()
而不是 ::table()
::table()
函数试图改变你的 table
我正在使用 Laravel 8。我正在尝试创建一个新的 table,而这个 table 在数据库中不存在,并且没有任何其他具有相同名称的 table。我做了迁移,这是迁移代码:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCertificateTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('certificate', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('vac_id');
$table->time('last_shot_date');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('vac_id')->references('id')->on('vaccination');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('certificate', function (Blueprint $table) {
//
});
}
}
做完后
php artisan migrate
这是我收到的完整错误消息:
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vaccinationappointme
nts.certificate' doesn't exist (SQL: alter table `certificate` add `id` bigint un
signed not null auto_increment primary key, add `user_id` bigint unsigned not nul
l, add `vac_id` bigint unsigned not null, add `last_shot_date` time not null, add
`created_at` timestamp null, add `updated_at` timestamp null)
at D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illumi
nate\Database\Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll
format the error
689▕ // message to include the bindings with SQL, which will make thi
s exception a
690▕ // lot more helpful to the developer instead of just the databas
e's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕
• A table was not found: You might have forgotten to run your migrations. You c
an run your migrations using `php artisan migrate`.
https://laravel.com/docs/master/migrations#running-migrations
1 D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table '
vaccinationappointments.certificate' doesn't exist")
2 D:\xampp\htdocs\Vaccination_Appointments\vendor\laravel\framework\src\Illum
inate\Database\Connection.php:485
PDOStatement::execute()
我试过:
php artisan migrate:refresh
还是一样的问题。我没用过模型。
如果要创建带有迁移的 table,您需要使用 ::create()
而不是 ::table()
::table()
函数试图改变你的 table