Laravel Migrations - Table Prefix Issue

10,004

Solution 1

In application->config->database.php set the prefix as follows

'mysql' => array(
'driver'   => 'mysql',
'host'     => 'localhost',
'database' => 'foodb',
'username' => 'root',
'password' => '',
'charset'  => 'utf8',
'prefix'   => 'ula_',       <-- this is where you need to set the table prefix
),

After setting this, migrate:reset and migrate it again I have done this way and its works perfect

Solution 2

On Laravel 5.4.*, I ended up creating the artisan command to add table prefix on certain tables (not globally) using below handle method.

public function handle()
{
    $this->tablePrefix = 'tmp_';

    // Set table prefix
    DB::setTablePrefix($this->tablePrefix);

    $data = [
        '--path' => [
            'database/prefixed-migrations' // Directory Path to migrations which require table prefix 
        ],
        '--database' => 'cli',
        '--force' => true
    ];

    $this->call('migrate', $data); // Next call the migration

    Model::reguard();
}

Hope this helps if anyone looking to prefix on certain tables without setting it globally.

Share:
10,004
darksoulsong
Author by

darksoulsong

Updated on June 04, 2022

Comments

  • darksoulsong
    darksoulsong almost 2 years

    I'm building a dummy site to test Laravel 3.x.

    I'm creating my site migrations right now. Everything was doing just fine until the following error showed up:

    SQLSTATE[42s02]: Base table or view not found: 1146 Table 'databasenamehere.prefix_laravel_migrations' doesn't exist
    

    The issue is that laravel all of a sudden started to prefix the 'laravel_migrations' table (when it is supposed to do it only with the other ones).

    I wonder if I'm doing something wrong or if it is a known issue.

    I'm trying to run the following migration (using the php artisan migrate application command):

    public function up()
    {
        Schema::create('siteinfo', function ($table) 
        {
            $table->engine = 'InnoDB';
            $table->string('name');
            $table->string('title')->nullable();
            $table->string('corp_name')->nullable();
            $table->string('corp_addr')->nullable();
            $table->string('corp_phone')->nullable();
            $table->string('corp_city')->nullable();
            $table->string('corp_state')->nullable();
            $table->string('corp_email')->nullable();
            $table->string('main_url')->nullable();
            $table->timestamps();
        });
    }
    

    Any help would be great.

    EDIT 1:

    • I noticed some minutes ago that my tables got no prefix at all, even with the "prefix" configuration set correctly on the config/database.php file.
    • Everything works fine if I remove the prefix. I know that I can set the prefix manually in every migration I run, but well...