未定义表:7 错误:关系“费用”不存在

Jul*_*ira 4 php postgresql laravel eloquent

由于我是西班牙语使用者,我用西班牙语编写了收入和支出的控制器和模型;而其余的都是英文的。我手动重命名和更改了路由、控制器、迁移甚至模型。当我运行 php artisan migrate:reset 这是我的错误。

未定义的表:7 错误:关系“费用”不存在(SQL:更改表“费用”删除列“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');
        }

    }
Run Code Online (Sandbox Code Playgroud)

移民:

    <?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

位置迁移:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

模型:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

希望您能够帮助我。

Ant*_*iro 6

当它说

relation "expenses" does not exist
Run Code Online (Sandbox Code Playgroud)

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

检查迁移的执行顺序。

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