laravel - 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 查看更多详细信息。
任何帮助将不胜感激。
编辑:整个错误输入错误。数据库名称已更正...
解决方案
改成Schema::table
方法Schema::create
中up()
。
推荐阅读
- windows - WMI 查询线程事件 - 无结果
- sql-server - 浮柱的状况
- node.js - Node Express Mongo API 返回空结果集
- python - Tkinter-如何在 ttk.Notebook() 中添加 Canvas/Frame
- ruby-on-rails - Errno::ENOSPC ActiveStorage::AnalyzeJob 设备上没有剩余空间 @ io_write - /tmp/ActiveStorage-24615
- python - python程序的桌面应用程序数据库
- go - 在 YML 中使用具有默认值的 env 变量
- javascript - 根据特定条件有条件地定义数组对象元素
- jquery - 安装了新的 wordpress 5.8 但为什么 Jquery 库不显示在二十一个 wordpress 主题的前端?
- python - 打印仅包含 2 个字符串的元素