【问题标题】:Laravel 8 SQLSTATE[42S02]: Base table or view not found: 1146 Table even though I am trying to create this tableLaravel 8 SQLSTATE [42S02]:未找到基表或视图:1146 表,即使我正在尝试创建此表
【发布时间】:2021-10-22 14:20:03
【问题描述】:

我正在使用 Laravel 8。我正在尝试创建一个新表,但该表在数据库中不存在,并且没有任何其他具有相同名称的表。我做了迁移,这是迁移代码:

<?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

仍然是同样的问题。我没用过模型。

【问题讨论】:

    标签: php laravel migration database-migration laravel-8


    【解决方案1】:

    如果要创建带迁移的表,需要使用::create()而不是::table()

    ::table() 函数试图改变你的表

    【讨论】:

    • 感谢它现在可以工作了!但我不明白为什么 Laravel 迁移会这样做?我输入:php artisan make:migration create_certificate_table --table=certificate。我在这里做错了吗?
    • 如果你给出 --table 选项,该命令认为你想改变表。但是如果你给 --create 选项,一切都会好起来的
    猜你喜欢
    • 2018-03-16
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多