【问题标题】:Undefined table: 7 ERROR: relation "expenses" does not exist未定义的表:7 错误:关系“费用”不存在
【发布时间】:2017-04-14 22:39:13
【问题描述】:

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

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

希望你能帮助我。

【问题讨论】:

  • 你有什么不明白的?
  • 当我尝试迁移到 psgql 时出现这些错误。
  • 错误信息很清楚——你没有一个名为费用的表。
  • 我实际上迁移了两个表,但是当我使用 migrate:status 时,我有一个 : N 在它们上面,而其他表在 Y 上。

标签: php postgresql laravel eloquent


【解决方案1】:

当它说

relation "expenses" does not exist

这通常发生在您的费用表必须在migration:reset 回滚CreateLocationsTable 之前被删除。

检查迁移的执行顺序。

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

【讨论】:

    【解决方案2】:
    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');
            });
    

    【讨论】:

      【解决方案3】:

      我遇到了这个错误。我必须通过将数据库的前缀添加到我的模型来解决它。有一种方法可以在数据库中更改它,并且它应该被更改。它是一些东西,比如 $user,public。这是架构配置文件。我登录时更改了我的,但由于某种原因,模型没有绑定到模式。所以我不得不在模型中指定它。而不是

          protected $table = 'table_name';
      

      我不得不这样做

          protected $table = 'schema_name.table_name';
      

      注意:通过 schema_name 或 table_name,我并不是指您将 schema_ 放在名称之后。这只是为了更容易阅读。 该模型位于 App/Models 文件夹下的模型文件夹中,具体取决于您使用的 Laravel 版本以及模型的组织方式。如果您的模型名称与表名称不同,那么您将需要放置受保护的 $table。但如果架构不存在,那么您将需要添加它。

      我的 DB_DATABASE_SECOND= 在 .env 文件中确实有这个设置。但不知何故,它仍然没有拿起前缀。

      但是是的。我几乎无法在任何地方找到答案,但我

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2020-01-03
      • 2014-10-24
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多