150 “Foreign key constraint is incorrectly formed”

I have a problem with migrating my table.

I have a users table that has a string id and the table was created before with SQL without a migration.

Later on, I created a table called surveys that has a foreign key to user_id with the following code .

hema::create('surveys', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users') ->onUpdate('cascade')->onDelete('cascade');

I am always getting the below error whenever I try to migrate it and I have no clue why is this happening. The id in table users is varchar 40 and so is the user_id.

SQLSTATE[HY000]: General error: 1005 Can't create table `d0372341`.`surveys` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `surveys` add constraint `surveys_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade on update cascade)

at D:xampp installhtdocsgame-dbvendorlaravelframeworksrcIlluminateDatabaseConnection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕ }

So if you can please help me with this I would really appreciate it.

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3k1Jn7i

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.