2

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...
darksoulsong
  • 13,988
  • 14
  • 47
  • 90
  • What do you have as `prefix` in `application` -> `config` -> `database.php` ? – Simone Apr 08 '13 at 17:58
  • I've prefixed my tables as "ma_". So, the error point to a "ma_laravel_migrations" table that, off course, does not exist... – darksoulsong Apr 08 '13 at 18:21
  • Have you tried running `php artisan migrate:install` first to setup the migration table for Artisan to use? – BenjaminRH Apr 08 '13 at 18:35
  • Yeah, I did that already. I made a couple migrations before this error start to appear. Actually, I ran 4 migs and at the fifth one, that error appeared. – darksoulsong Apr 08 '13 at 18:47
  • 1
    Does this happen consistently now, or only on the 5th migration? What happens if you rollback and run the migrations again? – Phill Sparks Apr 09 '13 at 18:46
  • @PhillSparks I just tried to do as you have suggested. I ran the "php artisan migrate:reset" command and the same error referencing the "ma_laravel_migrations" appeared. But there is something I noticed just now: all my tables have no prefix at all, even with the prefix option set correctly at the "config/database.php file". By setting a prefix, everytime I create a table, it should be created with that prefix, right? Real strange things going on here. – darksoulsong Apr 10 '13 at 14:12
  • 1
    @darksoulsong whilst I would expect a prefix to be applied in migrations, I can't actually find anything in the Schema source code that would confirm this assumption - that is to say it looks like Migrations/Schemas don't respect the prefix. Did you change/set the prefix since you first migrate:installed? – Phill Sparks Apr 10 '13 at 14:18
  • @PhillSparks No, I didn't. It is there from the very beginning. – darksoulsong Apr 10 '13 at 14:35
  • 1
    @PhillSparks Don't know what happened, but I dropped all the tables and reinstalled the laravel migrations... and everything is looking good now. TYVM for you help, sir. =) – darksoulsong Apr 10 '13 at 15:08

2 Answers2

7

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

Bala
  • 512
  • 9
  • 16
1

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.

rc.adhikari
  • 1,974
  • 1
  • 21
  • 24