try to replace the
$table->id();
to this
$table->bigIncrements('id');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I just created a new Laravel 8 project and running the migrations created during project creation. 3 of the four migrations run fine with no errors and the tables are created correctly. The CreateUsersTable migration crashes with the error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
The weird thing about this is, there are NO foreign keys involved.
Here's the migration - it's exactly as it came "out of the box":
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
The only thing I've modified since I created the new project is the .env file to get the project to connect to the database. Everything else is just as generated by Laravel Installer. Just prior to creating the new project, I reinstalled the Laravel Installer to ensure I have the latest version. No additional frameworks or packages at this point.
I've created numerous Laravel projects before and never run into any problems getting the default migrations to run in my development environment, but this is the first one I've done with Laravel 8.
Thanks for any help anyone can offer.
I found the exact cause of this mysterious foreign key violation. It came from a table in another database I was messing around with earlier, which had a foreign key pointing to a users table in my app database that I later dropped. I thought I dropped the tables from the other database as well, then totally forgot about it. But it was still lurking out there, outside of my tiny little sphere of attention.
Here's how I was able to track it down:
The message I included in my original post only said there was a foreign key violation, but didn't provide any specifics about exactly what foreign key it was referring to.
After more google searches, I found a way - thanks to Christof Mewes at xrstf.de - to get MySQL to give up its [expletive] secrets when it reports a foreign key violation. Immediately after the error occurs, in your mysql client, type the command:
mysql> SHOW ENGINE INNODB STATUS;
In my case, I got this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2021-10-19 21:20:53 0x7f3edc140700 Error in foreign key constraint of table mydb/billing:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT `fk_billing_users1` FOREIGN KEY (`users_id`) REFERENCES `appname_db`.`users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
The index in the foreign key in table is fk_billing_users1_idx
Please refer to http://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html for correct foreign key definition.
No wonder I couldn't find it. I was looking in my app database for foreign keys pointing to the users table, and it ended up being in some other database.
So, stupid mistake on my part, but I decided to share this in case anyone else benefit from this tip on debugging foreign key errors.
Please or to participate in this conversation.