Are the users and types tables created before articles?
Foreign keys not being created for some reason...
Hello there!
Once again I have a question and I don't see what I am doing wrong yet. For some reason I get an error back on running migrations, it states:
errno: 150 "Foreign key constraint is incorrectly formed"
For what I've found, that would mean most of the time that the types don't match from the referencing tables. But in my case, they actually do. The schema's are as follows:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Yes, the standard user table! ;) And the other table is this one:
Schema::create('articles', function (Blueprint $table) {
// Create table columns
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('type_id');
$table->dateTimeTz('published_at')->nullable()->default(null);
$table->dateTimeTz('unpublished_at')->nullable()->default(null);
$table->longText('content');
$table->timestamps();
$table->softDeletes();
// Set foreign keys for articles table
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('set null');
$table->foreign('type_id')
->references('id')->on('types')
->onDelete('set null');
});
So you could say, an almost copy of Jeffrey Way's tutorial! And yes, I do the exact same thing, but yet I still get the error message as stated above.
Even the collation and such are the same, what am I forgetting, as I don't see it at all. Keep in mind, both tables are created fresh when I run the migrations and I've already made sure that the order in which the migrations get run are also correct, by having adjusted the timestamps in the filenames.
I use MariaDB 10.3.20-MariaDB-1 from Debian default repositories and the complete configuration is as follows:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
It's the default connection settings indeed. ;) So I don't understand what goes wrong now? Any help would be welcome! The only thing I can think of is I try to create the foreign key the other way around, but that would suggest Jeffrey Way would do it wrong to in his tutorial (https://laracasts.com/series/laravel-6-from-scratch/episodes/30) at 8 minutes 12 seconds. Which seems unlikely to me, so I must miss something... The Laravel version I use is 6.7.0.
What a mess... last one before bed... can you run the migrations fresh without the FK constraints, then create a separate migration for the FK constraints only; run that using the --pretend option and run SQL result directly in your MySQL GUI. Finally get the status of a SHOW ENGINE INNODB STATUS; query and see if there are any clues there, especially in the LATEST FOREIGN KEY ERROR section of the output - it will typically be more descriptive than the generic errno 150...
Please or to participate in this conversation.