![](/img/trans.png)
[英]Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "contacts" does not exist
[英]SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories")
我昨天刚刚部署在 Heroku 上并连接到 Postgresql 数据库,从那时起我在 Heroku 的屏幕(和终端)上显示了这个有趣的错误:
SQLSTATE[42P01]:未定义表:7 错误:关系“类别”不存在第 1 行:从“类别”中选择 * ^(SQL:从“类别”中选择 *)
在我的终端中,在此错误下方,我有一个未定义的表错误,指出我的类别表不存在。 这太令人沮丧了,因为它确实存在并且就在那里! 有人可以帮忙吗? 有没有人有类似的问题?
尝试:
迁移一直运行到关系所在的故事表,然后停止运行。 故事正下方是类别表。 我不知道这对完善解决方案有多大帮助。
故事表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stories');
}
}
类别表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
故事模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Reviews;
use App\User;
use App\Category;
use App\ReadingList;
class Story extends Model
{
protected $guarded = [];
public function readingList()
{
return $this->belongsTo(ReadingList::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function reviews() {
return $this->hasMany(Reviews::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
类别型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Story;
class Category extends Model
{
protected $guarded = [];
public function story()
{
return $this->hasMany(Story::class);
}
}
已经关注这个好几天了,也许你们可以看到我看不到的东西。 非常感谢你。
那是因为您的类别表迁移在故事表之后运行,但是由于外键,故事表依赖于类别。 你需要做的是:
您应该更改迁移文件名中的日期
例子
2019_07_29_113341_create_categories_table
2019_07_30_113341_create_stories_table
较旧的日期时间将首先运行,
你应该迁移类别表然后故事
希望能帮到你
您必须考虑首先应该创建 Parent 迁移,然后应该建立关系。 首先创建“类别”迁移然后创建“故事”迁移。 我希望它有用..
您可以在这些场景中创建单个迁移文件。
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->string('title')->nullable();
$table->string('img')->nullable();
$table->timestamps();
});
Schema::create('stories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('story');
$table->date('published_on');
$table->integer('count_views')->default(0);
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedBigInteger("category_id");
$table->foreign('category_id')->references('id')->on('categories')->ondDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
Schema::dropIfExists('stories');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.