DigitalWM
DigitalWM

Reputation: 4516

Mysql InnoDB engine in Laravel

I am using laravel and laravel migration mechanism. I created tables and seted up foreign keys. But the tables are MyISSAM so no foreign keys are created. Where do I enable / configure this? (to change it to InnoDB and not in the mysql server).

Upvotes: 49

Views: 48571

Answers (9)

Aziz Badar
Aziz Badar

Reputation: 3

The best approach for those that don't use database.php) is to include on the .env file: DB_ENGINE=InnoDB. Remember to check if you have 'engine' => env('DB_ENGINE', null) on your database.php

Upvotes: 0

Akash
Akash

Reputation: 5012

I would recommend to update your Mysql to 5.5 or higher. The default storage engine for Mysql now is InoDB

Before MySQL 5.5.5, MyISAM is the default storage engine. (The default was changed to InnoDB in MySQL 5.5.5.) MyISAM is based on the older (and no longer available) ISAM storage engine but has many useful extensions.

http://dev.mysql.com/doc/refman/5.5/en/myisam-storage-engine.html

Once done, you can easily map relationships within the entity classes via Laravel

Upvotes: 3

Huy Nguyễn
Huy Nguyễn

Reputation: 27

Schema::create('users', function($table)
{
    $table->engine = 'InnoDB';

    $table->string('email');
});

Like document Laravel: https://laravel.com/docs/4.2/schema#storage-engines

P/s: Thanks @Nico Haase for reminding me to provide the link.

Upvotes: 2

Yousef Altaf
Yousef Altaf

Reputation: 2763

I found @ThomasLAURENT is the best solution but what about the existing tables I have in my database.

Working around.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ConvertTablesIntoInnoDB extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $tables = [
            'users',
            'products',
        ];
        foreach ($tables as $table) {
            DB::statement('ALTER TABLE ' . $table . ' ENGINE = InnoDB');
        }
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $tables = [
            'users',
            'products',
        ];
        foreach ($tables as $table) {
            DB::statement('ALTER TABLE ' . $table . ' ENGINE = MyISAM');
        }
    }
}

This will allow us to convert all the tables and roll-back them when I need.

Upvotes: 6

Tiago Gouvêa
Tiago Gouvêa

Reputation: 16740

Another approach (for whose that don't uses database.php) is to include on .env file:

DB_ENGINE=InnoDB

Remember to check if you have 'engine' => env('DB_ENGINE', null), on your database.php

Upvotes: 4

crynobone
crynobone

Reputation: 1814

You can set the engine inside Schema\Table closure.

Upvotes: 7

Thomas LAURENT
Thomas LAURENT

Reputation: 1681

You can edit your /config/database.php file, search for mysql entry and change:

'engine' => null,

to

'engine' => 'InnoDB',

This saves you from adding $table->engine = "InnoDB"; for each of your Schemas ;)

Upvotes: 133

srsajid
srsajid

Reputation: 1787

Define engine like this

  Schema::create("models", function(Blueprint $table) {
            $table->engine = "InnoDB";
  }

Upvotes: 14

RDK
RDK

Reputation: 4560

Use InnoDb tables on the server side it's best way to success. Use MySQL Workbench. It easy in Workbench. And, if you want read the native manual

Upvotes: 1

Related Questions