5

Laravel is throwing this error when I try to seed the database.

My table is institution_school and not institution_schools, what Laravel reports in the error.

 [Illuminate\Database\QueryException]
 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'eesdatabase.inst
 itution_schools' doesn't exist (SQL: insert into `institution_schools` (`sc
 hool_id`, `instituion_id`, `energy_id`, `year`, `updated_at`, `created_at`)
  values (38, 1, 1, 2005, 2014-07-04 19:38:41, 2014-07-04 19:38:41))

I try to drop database and again migrate and seed. I try to reset Laravel cache with "php artisan:cache clear"

Does anyone know how to fix this? Thanks

<?php    
class InstitutionSchool extends Eloquent {


    protected $table = "institution_school";        
    protected $guarded = array('id');

    public function school() {
        return $this -> belongsTo('School');
    }

    public function institutio() {
        return $this -> belongsTo('Institution');
    }

    public function energy() {
        return $this -> belongsTo('Energy');
    }    
}
?>

<?php
class InstitutionSchoolTableSeeder extends DatabaseSeeder {
    public function run() {
        DB::table('institution_school') -> delete();

        $faker = $this -> getFaker();
        $schools = School::all();
        $institutions = Institution::all();
        $energies = Energy::all();

        foreach ($schools as $school) {
            for ($i = 0; $i < rand(1, 5); $i++) {
                $year = $faker -> randomNumber(2000, 2015);

                InstitutionSchool::create(array(
                'school_id' => $school -> id, 
                'instituion_id' => 1,
                'energy_id' => 1, 
                'year' => $year));
            }

        }
    }

}
?>

<?php    
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateInstitutionSchoolTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('institution_school', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('institution_id')->unsigned();
            $table->integer('school_id')->unsigned();
            $table->string('year');
            $table->string('other_source_name')->nullable();

            $table->integer('energy_id')->unsigned()->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('institution_school', function(Blueprint $table)
        {
            Schema::drop('institution_school');
        });
    }    
}
Aleksandar
  • 636
  • 1
  • 7
  • 24
  • Now with code, it's much better. But honestly, I don't see any error. The explicit table declaration looks OK. – MightyPork Jul 04 '14 at 20:10
  • I'd just point out that ` Schema::drop('institution_school');` doesn't have to be in `Schema::table('in...`, but that's not the cause of your error. – MightyPork Jul 04 '14 at 20:13

3 Answers3

7

Try explicitly defining table name in your Model. It might work. But it is of course not a complete solution, just a hack to make it work :D

protected $table = "institution_school";
  • 1
    Don't get it - @Aleksandar, you ALREADY HAVE this in your model. How is it the solution? – MightyPork Jul 04 '14 at 20:14
  • It was commented before I paste it. On other tables I don't have that code and I think it's not important. This is the solution! – Aleksandar Jul 13 '14 at 20:37
0

Laravel assumes that you are naming your SQL tables lowercase with plural, and the models are PascalCase in singular. You may want to rename your table to include the "s".

John
  • 2,894
  • 2
  • 20
  • 25
  • The `protected $table = "institution_school";` piece is supposed to override that. Since this is a sort of pivot table with extra data, naming it with "s" would not make much sense. – MightyPork Jul 04 '14 at 20:16
0

In your InstitutionSchoolTableSeeder you have the following:

DB::table('institution_school') -> delete();

It should be:

DB::table('institution_school') -> truncate();

With ->delete() the table is gone, and it will throw the error. With ->truncate() the table will just be emptied, but will still exist so you can seed it.

TunaMaxx
  • 1,782
  • 12
  • 18