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

时间:2016-11-30 14:42:48

标签: php postgresql laravel eloquent

由于我是一名西班牙语演讲者,我用西班牙语写了收入和支出的控制器和模型;而所有其余的都是英语.. 我重命名并手动更改了路由,控制器,迁移甚至模型。 当我运行php artisan migrate时:重置这是我的错误。

  

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

希望你能帮助我。

2 个答案:

答案 0 :(得分:1)

当它说

relation "expenses" does not exist

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

检查迁移的执行顺序。

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

答案 1 :(得分:0)

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

将此更改为

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