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

标签 php postgresql laravel eloquent

因为我会说西类牙语,所以我用西类牙语写了收入和支出的 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/

相关文章:

Postgresql:错误:时区目录堆栈溢出

php - 在 Laravel 中迁移外键与 Eloquent 关系

ruby-on-rails - 多个 worker 的 Rails DB Performance

javascript - WordPress:从主页调用时使用 window.location 的 Javascript 重定向问题

PhpMailer 不发送邮件 - TLS 错误?

php - php mysql数据库连接错误

mysql - ansible 和 postgresql - 有没有办法使用 ansible 针对 postgresql 数据库运行 .sql 文件?

sql-server - 如何在 laravel 中格式化 sql server 的日期时间?

mysql - 如何使用加法和减法运算符在 Laravel 中构建 sum() 查询?

javascript - 使用从 PHP 生成的 HTML 调用的函数 AJAX 更新 MYSQL 数据库