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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40891205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:50:54  来源:igfitidea点击:

Undefined table: 7 ERROR: relation "expenses" does not exist

phppostgresqllaraveleloquent

提问by Julian Moreira

Since i am a spanish speaker, i wrote the controllers and models of income and expense in spanish; while all the rest were on english.. I renamed and changed manually Routes, Controllers, Migrations and even Models. And when i run php artisan migrate:reset this is my error.

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

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

未定义表:7 错误:关系“费用”不存在(SQL:更改表“费用”删除列“location_id”)**

I use psgql and laravel 5.3

我使用 psgql 和 laravel 5.3

This is my code:

这是我的代码:

    <?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');
        }

    }

Migration:

移民:

    <?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');
    }
}

Location Migration:

位置迁移:

<?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');
    }
}

Model:

模型:

<?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');
    }
}

Hope you can help me.

希望您能够帮助我。

回答by Antonio Carlos Ribeiro

When it says

当它说

relation "expenses" does not exist

It usually happens when your expenses table must have been dropped before migration:resetrolled back CreateLocationsTable.

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

Check the order of execution of your migrations.

检查迁移的执行顺序。

As you were trying to reset, to fix it now, open your database, drop all tables manually and execute migrateagain.

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

回答by Kiran Patel

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

change this to

将此更改为

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