Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lalev's avatar
Level 1

Laravel 5.4 and Eloquent migration with XAMPP

I'm trying to learn Laravel Eloquent. It's my first migration and it produces error. I'll try to describe it by pasting the relevant output of the things I've done:

The goal: To create new table 'author_metadatas'. The question: How to do it?

At first, I remove manually everything in the database as if I'm starting from scratch. Please note that I found solution for the problem, so author_metadatas exists, but I'm quite convinced my solution cannot be correct and it will break something later. Also note that I set 2017 in the date of my migration to 2016 out of desperation when I was trying to make it work. Turned out it didn't help.

# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 51 Server version: 10.1.22-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use library; Database changed MariaDB [library]> show tables; +-------------------+ | Tables_in_library | +-------------------+ | author_metadatas | | migrations | | users | +-------------------+ 3 rows in set (0.00 sec)

MariaDB [library]> drop table author_metadatas; Query OK, 0 rows affected (0.10 sec)

MariaDB [library]> select * from migrations; +----+-------------------------------------------------+-------+ | id | migration | batch | +----+-------------------------------------------------+-------+ | 1 | 2016_06_10_051345_create_author_metadatas_table | 1 | +----+-------------------------------------------------+-------+ 1 row in set (0.00 sec)

MariaDB [library]> drop table migrations; Query OK, 0 rows affected (0.09 sec)

MariaDB [library]> drop table users; Query OK, 0 rows affected (0.09 sec)

MariaDB [library]> show tables; Empty set (0.00 sec)

MariaDB [library]> quit

Let's check our migration files. We have three of them. The first two with dates from 2014 are the ones that seem to create the problem. They came with Laravel.

+++++@----- c:\xampp\htdocs\library # cd database\migrations

+++++@----- c:\xampp\htdocs\library\database\migrations # dir Volume in drive C has no label. Volume Serial Number is 722D-1AD2

Directory of c:\xampp\htdocs\library\database\migrations

06/10/2017 09:38 AM . 06/10/2017 09:38 AM .. 06/10/2017 09:38 AM 781 2014_1012000000_create_users_table.php 06/10/2017 09:38 AM 715 2014_10_12_100000create_password_resets_table.php 06/10/2017 09:38 AM 887 201706_10_051345_create_author_metadatas_table.php 3 File(s) 2,383 bytes 2 Dir(s) 15,130,337,280 bytes free

Start migration. The PDO Exception seems to stop the process of applying migrations in the middle.

+++++@----- c:\xampp\htdocs\library\database\migrations # cd ....

+++++@----- c:\xampp\htdocs\library

# php artisan migrate Migration table created successfully.

[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

Check MySQL database

+++++@----- c:\xampp\htdocs\library # mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 53 Server version: 10.1.22-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use library; Database changed MariaDB [library]> show tables; +-------------------+ | Tables_in_library | +-------------------+ | migrations | | users | +-------------------+ 2 rows in set (0.00 sec)

MariaDB [library]> select * from migrations; Empty set (0.00 sec)

MariaDB [library]> quit

So 'the solution'. Delete the migrations that came with Laravel.

+++++@----- c:\xampp\htdocs\library # php artisan migrate Migrating: 2017_06_10_051345_create_author_metadatas_table Migrated: 2017_06_10_051345_create_author_metadatas_table

+++++@----- c:\xampp\htdocs\library # php artisan migrate Nothing to migrate.

+++++@----- c:\xampp\htdocs\library

Thanks for the help in advance!

0 likes
5 replies
RayC's avatar

Create the migration with artisan:

php artisan make:migration author_metadatas

If you need a model you can create them both using:

php artisan make:model AuthorMetadata -m

Then edit the file located in the database/migrations directory.

Let artisan do the work for you.

Read more here: https://laravel.com/docs/5.4/migrations

Hope this helps.

Regards,

Ray

Lalev's avatar
Level 1

Thank you, the proposed fix works! I understand that developers are informed, so we can close this one :D

Please or to participate in this conversation.