首页 > 解决方案 > Illuminate\Database\QueryException SQLSTATE[42P01]: Undefined table: 7 ERROR: during migration

问题描述

我试图使用此处的代码在 laravel 中创建迁移。但不幸的是,它会弹出一个像这里给出的错误。我看到了一些我手动创建表的答案..但这与迁移的整个想法背道而驰..不是吗?

迁移文件 2018_05_05_203731_create_cities_table 在这里:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCitiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('cities', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->text('Name');
            $table->json('info');
            $table->integer('country_id');
            $table->float('lat');
            $table->float('lon');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('cities', function (Blueprint $table) {
            //
        });
    }
}

产生的错误在这里:

C:\Users\think\Documents\NZ\blog>php artisan 迁移

Illuminate\Database\QueryException : SQLSTATE[08006] [7] FATAL: 数据库“tripplan1”不存在(SQL: select * from information_schema.tables where table_schema = public and table_name = migrations) es not exist")在 C:\Users \think\Documents\NZ\blog\vendor\laravel\framework\src\Illuminate\Databasee\Connection.php:458\Connection.php:664 660| // 如果尝试运行查询时发生异常,我们将格式化错误
e\Connection.php:458 661| // 包含与 SQL 的绑定的消息,这将使这个异常成为 662| // 对开发人员更有帮助,而不仅仅是数据库的错误。663| catch (Exception $e) {

664| throw new QueryException(665| $query, $this->prepareBindings($bindings), $e 666|); 667| }

C:\Users\think\Documents\NZ\blog>php artisan migrate 迁移表创建成功。

Illuminate\Database\QueryException:SQLSTATE[42P01]:未定义表:7 错误:关系“城市”不存在(SQL:改变表“城市”添加列“id”序列主键不为空,添加列“created_at”时间戳( 0) 没有时区为空,添加列“updated_at”时间戳(0)没有时区为空,添加列“名称”文本不为空,添加列“信息”json 不为空,添加列“国家 ID”整数不为空,添加列“lat”双精度不为空,在 C:\Users\think\Documents\NZ\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660 处添加列“lon”双精度不空) | // 如果尝试运行查询时发生异常,我们' ll格式化错误661| // 包含与 SQL 的绑定的消息,这将使该异常成为 662| // 对开发人员更有帮助,而不仅仅是数据库的错误。663| 捕获(异常 $e){

664| throw new QueryException(665| $query, $this->prepareBindings($bindings), $e 666|); 667| } 668|

                                          Exception trace:

1 PDOException::("SQLSTATE[42P01]: 未定义表:7 错误:关系“城市”不存在") C:\Users\think\Documents\NZ\blog\vendor\laravel\framework\src\Illuminate\Database \Connection.php:458 2 PDOStatement::execute() C:\Users\think\Documents\NZ\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

请使用参数 -v 查看更多详细信息。

任何帮助将不胜感激。

编辑:整个错误输入错误。数据库名称已更正...

标签: laravellaravel-5migrationdatabase-migration

解决方案


改成Schema::table方法Schema::createup()


推荐阅读