Undefined table: 7 ERROR: relation "expenses" does not exist
Solution 1
When it says
relation "expenses" does not exist
It usually happens when your expenses table must have been dropped before migration:reset
rolled back 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 migrate
again.
Solution 2
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');
});
Julian Moreira
Updated on July 09, 2022Comments
-
Julian Moreira almost 2 years
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.
Undefined table: 7 ERROR: relation "expenses" does not exist (SQL: alter table "expenses" drop column "location_id")**
I use psgql and 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.