gpt4 book ai didi

php - 未定义表 : 7 ERROR: relation "expenses" does not exist

转载 作者:可可西里 更新时间:2023-11-01 01:15:11 25 4
gpt4 key购买 nike

因为我会说西类牙语,所以我用西类牙语写了收入和支出的 Controller 和模型;而其余的都是英语..我手动重命名并更改了路由、 Controller 、迁移甚至模型。当我运行 php artisan migrate:reset 时,这是我的错误。

Undefined table: 7 ERROR: relation "expenses" does not exist (SQL: alter table "expenses" drop column "location_id")**

我使用 psgql 和 laravel 5.3

这是我的代码:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Expense extends Model
{

protected $fillable = ['id', 'description', 'quantity'];


public function locations()
{

return $this->hasMany('App\Location');

}
public function icons()
{

return $this->hasMany('App\Icon');
}
public function types()
{

return $this->hasMany('App\Type');
}
public function stores()
{

return $this->hasMany('App\Store');
}

}

迁移:

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateExpensesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('expenses', function (Blueprint $table) {
$table->increments('id');
$table->float('quantity');
$table->string('description');
$table->integer('location_id')->unsigned();
$table->integer('icon_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('store_id')->unsigned();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('expenses');
}
}

位置迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLocationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('locations', function (Blueprint $table) {
$table->increments('id');
$table->string('address');
$table->float('lat');
$table->float('long');
$table->integer('store_id')->unsigned();
$table->timestamps();

$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
});

Schema::table('expenses', function (Blueprint $table) {
$table->foreign('location_id')->references('id')->on('locations')->onDelete('cascade');
});
}

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

Schema::dropIfExists('locations');
}
}

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Location extends Model
{

protected $fillable = ['id', 'address', 'lat', 'long'];


public function expenses()
{

return $this->belongsTo('App\Expense');
}

public function stores()
{

return $this->hasOne('App\Store');
}
}

希望你能帮助我。

最佳答案

当它说

relation "expenses" does not exist

当您的费用表必须在 migration:reset 回滚 CreateLocationsTable 之前被删除时,通常会发生这种情况。

检查迁移的执行顺序。

当您尝试重置时,现在修复它,打开您的数据库,手动删除所有表并再次执行 migrate

关于php - 未定义表 : 7 ERROR: relation "expenses" does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40891205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com