简体   繁体   中英

Laravel Migration to add foreign key not working

I am back adding foreign keys to some tables but this one is not working.

Schema::table('users', function(Blueprint $table){
    $table->integer('account_type')->unsigned()->change();
    $table->foreign('account_type')
        ->references('id')
        ->on('account_types');
});

It throws these 3 errors in terminal:

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_account_type_foreign` foreign key (`account_type`) references `account_types` (`id`))


[Doctrine\DBAL\Driver\PDOException]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint


[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Any idea what I need to do to fix this? I wan't to be able to click on the account_type field in my users table and be taken to the related account type in the account_types table whilst using SequelPro.

If we assume that you've already created account_types and users , then this is what you could do.

Run this command to create a new migration.

php artisan make:migration add_account_type_to_users

Then add this to your up function, what this will do is update the already existing users table, with adding a column account_type and then adding the foreign key to the account_types table.

public function up()
{
    Schema::table('users', function(Blueprint $table) {
        $table->integer('account_type')->unsigned();
        $table->foreign('account_type')->references('id')->on('account_types');
    });
}

Then add this to your down function, to allow the migration to run smoothly incase of future changes and/or a refresh.

public function down()
{
    Schema::table('users', function(Blueprint $table) {
        $table->dropColumn('account_type');
    });
}

I am assuming your issue is with the dating of the migration files - I would advice you to go over them and make sure they get executed in the right order.

Your table 'account_type' need to be created before 'users' table.

Basically on Laravel, users table is the first created, to resolve it you can put all schema on One migration file. or you need to change the date in the name of the users file, because laravel execute migrations in name order.

I make my migration like this :

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DatabaseContent extends Migration
{

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    $connection = config('database.default');

    Schema::connection($connection)->create('account_type', function 
    (Blueprint $table) {
   // Own Method
  }
    Schema::connection($connection)->create('users', function 
(Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

I suggest that you create a migration specifically to add the foreign key constraint. Migrations should be created in such a way that each change to your database is a NEW migration. That way if something goes wrong, you can roll it back. "Back adding" as you said you were doing is likely the root of the problem. Like some users have suggested, the order in which your migrations are created is critical to how the database is built. And you must have created both participating tables in order to create a foreign key constaint on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM