2

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

1
  • If you don't have data you need to keep, run php artisan migrate:fresh first. Sounds like you have migration records in the db. This will drop all your tables so be warned. Commented Feb 8, 2018 at 10:33

4 Answers 4

1

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

composer du means composer dump-autoload ?
@SuYang it's a shortcut for composer dump-autoload. You can use both.
It's still not working. Really confused right now! :/.. I get this: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
@harunB10 did you get any errors after doing this? Also, make sure you don't have currency table before executing the migration.
1

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

1 Comment

Still not working. Don't know what I am missing here... I did composer du also...
0

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.

Comments

0

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',
    //..
],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.