brainless
brainless

Reputation: 5819

Table not found error while seeding DB in Laravel

I am using way/generators package for generating pivot table in Laravel 4.2. I am getting the below error which specifies Table 'pricom.roleuser' doesn't exist, but the table name in my db is 'pricom.role_user'. All other migrations and seeds are working fine except for role_user table.

[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pricom.roleuser' doesn't exist

My master DatabaseSeeder looks like below as suggested by Jeff Way

<?php

/**
 * Class DatabaseSeeder
 */
class DatabaseSeeder extends Seeder
{

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        $tables = [
            'Users',
            'Designations',
            'Departments',
            'News',
            'Organizations',
            'Roles',
            'RoleUser'
        ];

        Eloquent::unguard();

        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        foreach ($tables as $table) {
            DB::table($table)->truncate();
        }

        foreach ($tables as $table) {
            $this->call($table . 'TableSeeder');
        }

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
}

RoleUser Model

<?php

class RoleUser extends \Eloquent {
    protected $fillable = [];

    protected $table='role_user';

}

RoleUser Seeder

<?php

// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;

class RoleUsersTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();

        $roleUsers = [
            [
                'role_id'=>1,
                'user_id'=>1
            ],
            [
                'role_id'=>2,
                'user_id'=>2
            ],
            [
                'role_id'=>3,
                'user_id'=>3
            ]
        ];


        foreach ($roleUsers as $roleUser) {
            RoleUser::create($roleUser);
        }

        foreach(range(1, 300) as $index)
        {
            RoleUser::create([
                'role_id'=>4,
                'user_id'=>$index+3
            ]);
        }
    }
}

Please help me fix this.

Upvotes: 0

Views: 1339

Answers (2)

brainless
brainless

Reputation: 5819

I fixed this by making some tweeks in my DatabaseSeeder.php. Thanks @RobbieP for your suggestion.

<?php

/**
 * Class DatabaseSeeder
 */
class DatabaseSeeder extends Seeder
{

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $tables = [
            'User',
            'Designation',
            'Department',
            'News',
            'Organization',
            'Role',
            'RoleUser'
        ];

        Eloquent::unguard();

        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        foreach ($tables as $table) {
            DB::table(with(new $table)->getTable())->truncate();
        }

        foreach ($tables as $table) {
            $this->call(str_plural($table) . 'TableSeeder');
        }

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');
    }
}

Upvotes: 0

RobbieP
RobbieP

Reputation: 698

Looks like you're trying to truncate a table called RoleUser which doesn't exist. You need to get the name of the table from the class. Try changing this bit in your database seeder:

foreach ($tables as $table) {
        DB::table(with(new $table)->getTable())->truncate();
}

Upvotes: 1

Related Questions