harunB10
harunB10

Reputation: 5207

Laravel migration doesn't add table in DB

I added a new table in migration in file 2018_02_08_094356_create_currency_table.php..

And this is the code:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCurrencyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currency', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('usdeur', 15, 2);
            $table->decimal('usdchf', 15, 2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('currency');
    }
}

When I run php artisan migrate, there is only default laravel users (and migration) table in my DB. No currency one.

What could be the reason?

EDIT

Everything was fine with migrations. The problem was this:

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Solution is to add this in AppServiceProvider.php

public function boot()
{
    Schema::defaultStringLength(191);
}

Source: https://laravel-news.com/laravel-5-4-key-too-long-error

Upvotes: 2

Views: 1768

Answers (4)

Roshan Twanabasu
Roshan Twanabasu

Reputation: 198

Laravel 5.4 uses utf8mb4 by default. One way to fix this error, is to default back to utf8.

Go to your config\database.php and change the database charset to uft8 and the collation to utf8_unicode_ci:

'mysql' => [
    //..
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    //..
],

Upvotes: 0

Prashant Prajapati
Prashant Prajapati

Reputation: 1015

open AppServiceProvider.php write the below code:

public function boot()
{
    Schema::defaultStringLength(191);
}

The table name must be plural rollback your migration using

php artisan migrate:rollback

or

manually delete the table from your database and migration table and try the below code:

migration command:
php artisan make:migration create_currencies_table

Code:

class CreateCurrenciesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currencies', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('usdeur', 15, 2);
            $table->decimal('usdchf', 15, 2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('currencies');
    }
}

now run

php artisan migrate

Upvotes: 1

sysummery
sysummery

Reputation: 96

All the migrations record are recorded in table migrations.It is indicate that you created it before.So delete the records about currencies in table migrations and run php artisan migrate again. what's more,refresh your database and check if the table currencies has created.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You need to run composer du to register the migration class first.

Then run php artisan migrate command to execute a new migration.

Upvotes: 1

Related Questions